Not totally sure this will help with your error, as i haven't come across it yet.
But for fields that may be empty I also have a blank = True and null = True. This is to tell the DB that it is acceptable to have no value in the field. ie, i'm using the following to choose add users to a private forum category: allowedUsers = models.ManyToManyField(User, verbose_name=_('Members'), blank=True, null=True, help_text=_('Select Users that are allowed to view this private group'), limit_choices_to={'is_staff': False} ) On Feb 4, 12:07 pm, GeneralMean <marcin.wtorkow...@gmail.com> wrote: > This is a huge one: > > I want to implement an optional m2m realtionship in django admin > application, so it is possible to create a new Game (model description > below) without choosing any tags associated with it. By default django > form validation returns an error when I try to do such thing. So I > created a new form (GamesAdminModelForm) and specified tag field as > "required=False". It works quite good, to the point when I want to add > some tags "by hand", after user submited the form. For example user > creates new Game, and after successful validation I want to add some > common tags to it. > > The problem is, there is, none of the relations to newly created game > gets saved in DB. No matter what I do, I tried to save it in Game > model method save(), tried to do that in save_model() method of > GamesAdmin (of course after calling obj.save(). Code returns no > exception. I even tried to inject some tags to form in save_model > method, and then re-saving the form - didn't work out well. > > The misterious thing of all, is that when I use function shown below, > everything works allright, to the point when I delete "assert Null" > command. What's with that?? When code throws exception m2m > relationships show up in database, when code DOESN'T throw exception > DB m2m relationship table remains empty. > > def save_model(self, request, obj, form, change): > obj.save() > #get some tag > tag = Tag.objects.get(pk=1) > #add relationship to new object > obj.tag.add(tag) > assert Null > > Hope someone gonna find the right answer. > > Simplified models: > > #in models.py > class Game(models.Model): > id = models.AutoField(primary_key=True, db_column='grg_id') > tags = models.ManyToManyField(Tag, related_name='g_tags') > > class Tag(models.Model): > id = models.AutoField(primary_key=True, db_column='grt_id') > tag = models.CharField(max_length=255, unique=True, > db_column='grt_tag') > tag_url = models.CharField(max_length=255, blank=False, > db_column='grt_tag_url') > tag_keyword = models.CharField(max_length=255, blank=False, > db_column='grt_tag_keyword') > > #in admin.py > class GamesAdmin(admin.ModelAdmin): > form = GamesAdminModelForm > > admin.site.register(Game, GamesAdmin) > admin.site.register(Tag) > > #forms.py > class GamesAdminModelForm(forms.ModelForm): > cover = forms.ImageField(widget=ImageWithThumbnailWidget, > required=False) > tag = forms.ModelMultipleChoiceField(queryset=Tags.objects.all > ().order_by('tag'), required=False, widget=FilteredSelectMultiple > (verbose_name='Tagi', is_stacked=True)) > > class Meta: > model = Game --~--~---------~--~----~------------~-------~--~----~ 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 django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---