N
N
Nevars2014-05-02 10:45:02
Django
Nevars, 2014-05-02 10:45:02

How to add the ability to change one field from another class in django to the admin panel?

Good afternoon. There is such a class:

class CustomerAdmin(admin.ModelAdmin):
    fields = ['user', 'address', 'phone']

And there is this one:
class OrdersAdmin(admin.ModelAdmin):
    fields = ['customer', 'date', 'amount_bottle', 'price', 'statusOrder']
    list_filter = ['date']
    date_hierarchy = 'date'

It is required to display the address field from the first class from the second class. How to do it?
PS The models are related like this:
class Customer(models.Model):
    user = models.ForeignKey(User)
    address = models.CharField(max_length=100)
    phone = models.IntegerField(max_length=11)

    def __unicode__(self):
        return self.user.first_name + ' ' + self.user.last_name


class Order(models.Model):
    customer = models.ForeignKey(Customer)
    date = models.DateTimeField()
    amount_bottle = models.IntegerField(default=0)
    price = models.IntegerField(default=0)
    statusOrder = models.CharField(max_length=15, default='booked')

    def __unicode__(self):
        return self.customer.user.first_name + ' ' + self.customer.user.last_name

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
aruseni, 2014-05-20
@aruseni

You can easily display it:

class OrdersAdmin(admin.ModelAdmin):

    fields = [
        'customer',
        'customer_address',
        'date',
        'amount_bottle',
        'price',
        'statusOrder'
    ]
    readonly_fields = ['customer_address',]
    list_filter = ['date']
    date_hierarchy = 'date'

    def customer_address(self, obj):
        return obj.customer.address

    customer_address.short_description = "Customer’s address"

But editing is more difficult. Imagine, for example, such a situation - let's say you added a field to this form that is stored in the address field of the associated Customer model object. But here's the thing: the user changed the value of the Customer field in the process of editing the field, and therefore the Order object is already associated with another customer. If you save the address anyway, it will turn out that you will overwrite the value already for another object, and this may be inappropriate. If you do not save - again, this may not be the expected behavior (maybe the user wanted to save exactly such an address for a new client).
Here I see the following solutions: either prohibit editing the client, or hide the field with the address if the client is modified, or even dynamically (using JS) load a new address when the client changes (and block saving the order until the address is loaded).
If one of these options suits you, then you need to edit the form used for the order editing page, and supplement the processing of this form so that the value of the address field is stored in the address field of the associated Customer object.
Good luck!
PS A note on style: better not OrdersAdmin, but OrderAdmin (especially since Customer is written in the singular).

S
Stepan Krapivin, 2014-06-11
@xevin

Most likely you need InlineModelAdmin

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question