views:

257

answers:

0

Hey all,

I have an issue where a particular modelform whose object is a many-to-many field pulls up the first entry there by default when passed to a modelformset_factory. An example follows:

Say I have an Order object that has a many-to-many relationship with a Group. I have a form whose only editable field is a single input box with the Group ID number. I use a modelformset_factory to get a collection of these forms. It looks like so:

sorted_orders = Order.order_by('-group__group_id')
Formset = modelformset_factory(Order, form=OrderRunForm, extra=0)
tilling_forms = Formset(queryset=sorted_orders)

However, if an object has more than one group, say group 3, group 5 and group 8, the field output by the modelformset_factory forloop in my template is '3'. The sort says to order by group_id descending, yet the number in the box remains the first group. I want to edit particular entries in that field, not just the first one. Is there a way to do this?

Here's the order model and order form:

# MODELS
class Order (m.Model):
customer  = m.ForeignKey('auth.User')
order_number = m.CharField(max_length=12, db_index=1, unique=1)
    #(snip...)
created_at  = m.DateTimeField(auto_now_add=1, db_index=1)
updated_at  = m.DateTimeField(auto_now=1, db_index=1)
group   = m.ManyToManyField('Group', blank=1, null=1)
objects   = OrderManager()

class Meta:
 ordering = ('-updated_at', '-created_at', '-id')

@m.permalink
def get_absolute_url(self):
 return ('order_detail', (), {'order_number':self.order_number})

model_name  = property(__unicode__)



class Group (m.Model):
group_id  = m.IntegerField(db_index=1)
created_at  = m.DateTimeField(auto_now_add=1, db_index=1)
run_at   = m.DateTimeField(blank=1, null=1, db_index=1)
objects   = GroupManager() 

def __unicode__(self):
 return self.short_label()



# FORM
class OrderRunForm (form.ModelForm):
group_id = f.IntegerField(required=0)

Thanks in advance,

J