views:

87

answers:

1

You have models:

class Order(Model):
 date = DateField(editable = False, auto_now_add=True)
 status = CharField(max_length = 1, choices = STATUS, default = 'N')
 profile = ForeignKey(Profile, related_name = 'orders', blank = True, null = True)
 shipping = ForeignKey(Shipping, related_name = 'orders', blank = True, null = True)
 address = ForeignKey(Address, related_name = 'address_orders', blank = True, null = True)
 company = ForeignKey(Company, related_name = 'company_orders', blank = True, null = True)

class Address(Model):
 address_profile = ForeignKey(Profile, related_name = 'addresses')
 city = CharField(max_length = 256, blank = True, null = True)
 street = CharField(max_length = 256, blank = True, null = True)
 zipcode = CharField(max_length = 10, blank = True, null = True)
 phone = CharField(max_length = 23, blank = True, null = True)

class Company(Address):
 company_profile = ForeignKey(Profile, related_name = 'companies')
 name = CharField(max_length = 256, blank = True, null = True)
 company_id = CharField(max_length = 256, blank = True, null = True)

How do you create OrderForm for specified profile? With this one

class OrderCheckoutForm(forms.ModelForm):
 class Meta:
  model = Order

I get a form with all addresses and companies in option. I'd like to limit them to related with it's profile. Is there any simple solution?

Thanks in advance, Etam.

+2  A: 

You can feed a queryset to your ModelChoiceField (which is what ModelForms use for ForeignKeys; you can find it in django/forms/models.py). Something like,

class OrderCheckoutForm(forms.ModelForm):
    profile = models.ModelChoiceField(queryset = Profile.objects.filter(...))

    class Meta:
        model = Order

Of course, your filtering criterion will depend on what you are trying to accomplish, which isn't 100% clear from your original post.

Jeff Bradberry