开发者

Django model relationships

开发者 https://www.devze.com 2023-03-24 04:41 出处:网络
I have 3 Django model classes: 1. Customer - contains multiple billing and shipping addresses 2. Address - can be billing or shipping address

I have 3 Django model classes:

1. Customer - contains multiple billing and shipping addresses

2. Address - can be billing or shipping address

3. Order - contain reference to 1 customer, 1 billing address and 1 shipping addresses chosen by the customer.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号