0

My problem relates to this question: Default ordering for m2m items by intermediate model field in Django

class Group(models.Model):
   name = models.CharField(max_length=128)
   _members = models.ManyToManyField(Person, through='Membership')
   @property
   def members(self):
       return self._members.order_by('membership__date_joined')

   def __unicode__(self):
       return self.name

I used the best answer's solution as you see here, however, it broke my model form that's based on the group model.

When I submit the form, I get _members is required in my model form's error list since the field is required and can no longer submit forms based on this model.

The best answer in the prior question suggests a way to mimic the behavior of the field using the property. How would I go about doing this to completely hide _members from the model form?

Thanks, Pete

Community
  • 1
  • 1
slypete
  • 5,538
  • 11
  • 47
  • 64
  • 1
    It is good to link to a related question, but it is so helpful to have you state exactly what you had, what you have, and what the error you're getting is. – David Berger Feb 01 '10 at 17:39
  • Can you post what your Form looks like as well? This may help us narrow it down. – Jack M. Feb 01 '10 at 20:14

1 Answers1

0

If it's a one-off, you can exclude the _members field when you create the modelform:

class GroupForm(ModelForm):
    class Meta:
        model=Group
        exclude = {'_members',}

If you do this a lot, you might consider creating a subclass of ModelForm and override the init method to automatically exclude properties starting with an underscore.

Sam
  • 607
  • 1
  • 8
  • 13
  • I see that you're trying to deal with a django bug. I suspect that a property is not what you want because ModelForms have no idea what a python property is. To actually deal with this, you may need to add a custom field to the modelform and then populate the members choices by overriding the init method, such as in this example: http://stackoverflow.com/questions/1387431/django-model-modelform-how-to-get-dynamic-choices-in-choicefield – Sam Feb 01 '10 at 19:22