Hello, This is question my get long so I'll post the short version then the longer more detailed version:
The short: My goal is provide the user with an autocomplete AJAX widget to quickly look up ISBNs. The ISBNs will end up in a text field in HTML to be sent back to the backend where I'd like it split and save in the ManyToMany field. As a first deep foray into newforms I'm getting stuck at multiple levels. The long: I have a Newsletter model with some basic fields. I have a Special model with FK to Newsletter and ManyToMany to books for book specials to be included with the newsletter. Since the Book model contains 16k + records the default admin for this takes forever to load and render which makes it unusable. I found a django snippet that I tried to adapt to create a CommaSeparatedBooks(Field|Input) here: http://www.djangosnippets.org/snippets/595/ >From there I'm trying to create a ModelForm from my Special model and override the book field to use my custom Field. Then I'm providing the FormSet to the StackedInline class to use. At this point I'm getting an error when I load the admin: Error while importing URLconf 'anglers.urls': 'ModelFormOptions' object has no attribute 'many_to_many'. So I've decided to come here for help and try to see where I've gone astray. See code below. ## admin.py from django.contrib import admin from django.db.models.fields.related import ManyToManyField from django.newforms import ModelForm from django.newforms.fields import Field from django.newforms.models import modelformset_factory from django.newforms.util import ValidationError from django.newforms.widgets import Input from anglers.book.models import Book from anglers.newsletter.models import Newsletter, Special # Widgets and Fields class CommaSeparatedBooksInput(Input): input_type = 'text' def render(self, name, value, attrs=None): if value is None: value = '' elif isinstance(value, (list, tuple)): value = ', '.join([book.isbn for book in value]) return super(CommaSeparatedBooksInput, self).render(name, value, attrs) class CommaSeparatedBooksField(Field): widget = CommaSeparatedBooksInput def clean(self, value): super(CommaSeparatedBooksField, self).clean(value) if not value: return '' if isinstance(value, (list, tuple)): return value isbns = set(value.split(',')) isbns = set([isbn.strip() for isbn in isbns]) books = list(Book.objects.filter(isbn__in=isbns)) unknown_books = isbns ^ set([book.isbn_clean for book in books]) if unknown_books: raise ValidationError('Book ISBN values not found: %s' % str(unknown_books)) return books # ModelForms and FormSets class SpecialBookForm(ModelForm): books = CommaSeparatedBooksField() class Meta: model = Special SpecialBookFormSet = modelformset_factory(SpecialBookForm) # ModelAdmins with Inlines class SpecialAdmin(admin.ModelAdmin): list_display = () search_fields = () ordering = () class SpecialBookInline(admin.StackedInline): model = Special formset = SpecialBookFormSet class NewsletterAdmin(admin.ModelAdmin): list_display = ('start_date', 'end_date') ordering = ('-start_date',) inlines = (SpecialBookInline,) admin.site.register(Newsletter, NewsletterAdmin) admin.site.register(Special, SpecialAdmin) ## models.py class Newsletter(models.Model): message = models.TextField() start_date = models.DateField(default=datetime.today) end_date = models.DateField() def __unicode__(self): return u"Newsletter from %s to %s" class Special(models.Model): newsletter = models.ForeignKey(Newsletter) header = models.CharField(max_length=200, core=True) message = models.TextField() books = models.ManyToManyField(Book) Thanks for any guidance. -Rob --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---