From the descriptions you provided, I believe the first one works. To clarify I would like to display the the results of the calculation in the admin.py.
The Djando docs confuse me. There are 4 examples after list_display. I was using list_display in my current admin.py however I don’t know what, if anything to use for the gateProfit return value I started with. That as an aside, using the 4th example but passing a decimal value
from django.contrib import admin
from django.db import models
class Person(models.Model):
name = models.CharField(max_length=50)
birthday = models.DateField()
@admin.display(description='Birth decade')
def decade_born_in(self):
return '%d’s' % (self.birthday.year // 10 * 10)
class PersonAdmin(admin.ModelAdmin):
list_display = ('name', 'decade_born_in')
is all of this code in the admin.py or is the example showing both the model.py and the admin.py?
the first class refers to models.Model, that confuses me.
Secondly, if the code is all in the admin.py then the calculation is done in admin.py and not the model so I can remove
That’s because there are four different, separate, and distinct sources of data that can be included in the list display. Those four examples demonstrate those four sources. You pick whichever one you need to use.
Which example? Depending on the case, it’s one or the other. They are two different sources handling two different situations.
So in your current situation, I think you’ve already recognized that the first case doesn’t apply.
Your choice among the other three is relatively arbitrary. Your selection among them would usually depend upon other factors. For example, if you need to use that calculation elsewhere in your system, you would likely prefer to have it in the Model, to be used in multiple locations.
I’m entering in values in the admin section currently. As I get to it it will be in a form from a webpage. That doesn’t matter though, just for context. I will leave the calc function in the model. So I’ll use the last example.
from django.contrib import admin
from django.db import models
class Person(models.Model):
name = models.CharField(max_length=50)
birthday = models.DateField()
@admin.display(description='Birth decade')
def decade_born_in(self):
return '%d’s' % (self.birthday.year // 10 * 10)
class PersonAdmin(admin.ModelAdmin):
list_display = ('name', 'decade_born_in')
If that’s good I’ll ask another question. So far so good?
Notice the difference in how the earlier example for decade_born_in accesses the birthday field within the model with how you’re trying to access the fields within the model in gameProfit.