How do I create an Order class that would render in the admin page so that users can choose which shippi开发者_运维技巧ng address to use?
The Address class has a property called name so that the user can assign short names like "HQ" or "NY Branch". I want users to be able to select the shipping address from a drop-down list using the short names. I'm a slow noob.
UPDATE
CODE
Order model:class Order(models.Model):
customer = models.ForeignKey(Customer)
shipping_address = models.CharField(max_length=80)// Should be a drop list based on the customer above
...
Customer model:
class Customer(models.Model)
name = models.CharField(max_length=80)
username = models.CharField(max_length=12)
password = models.CharField(max_length=12)
...
def __unicode__(self):
return self.name
Address model:
class Address(models.Model):
name = models.CharField(max_length=80, help_text='Easy to remember name like "HQ"')
customer = models.ForeignKey(Customer)
address_type = models.CharField(max_length=12, choices=ADDRESS_TYPES,)
street_1 = models.CharField(max_length=100)
street_2 = models.CharField(max_length=100)
...
def __unicode__(self):
return self.name
Thanks in advanced!
From the docs:
The
__unicode__
method of the model will be called to generate string representations of the objects for use in the field's choices...
So, override Address.__unicode__()
.
Try to look at this questions:
How to work with dynamic data in Admin Panel?
django ForeignKey model filter in admin-area?
I think they are quite similar to your question. All you need is to set choices in admin.py from Address model.
精彩评论