Answer the question
In order to leave comments, you need to log in
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']
class OrdersAdmin(admin.ModelAdmin):
fields = ['customer', 'date', 'amount_bottle', 'price', 'statusOrder']
list_filter = ['date']
date_hierarchy = 'date'
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
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"
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question