Answer the question
In order to leave comments, you need to log in
Please explain where does 'obj' come from in the 'get_image' method?
class Movie(models.Model):
title = models.CharField('Title', max_length=100)
tagline = models.CharField('Slogan', max_length=100, default='')
descriptions = models.TextField('Description' )
poster = models.ImageField('Poster', upload_to='movies/')
year = models.PositiveSmallIntegerField('Release date', default=2019)
country = models.CharField('Country', max_length=30)
directors = models .ManyToManyField(Actor, verbose_name='Director', related_name='film_director')
actors = models.ManyToManyField(Actor, verbose_name='Actors', related_name='film_actor')
genres = models.ManyToManyField(Genre, verbose_name='Genres')
world_premiere = models.DateField('World Premiere', default=date.today)
budget = models.PositiveIntegerField('Budget', default=0, help_text='Indicate the price in dollars')
fees_in_usa = models.PositiveIntegerField('Fees in USA', default=0, help_text='Price in USD')
fees_in_world = models.PositiveIntegerField('Fees in the World', default=0, help_text='Price in USD')
category = models.ForeignKey(Category, verbose_name ='Category', on_delete=models.SET_NULL, null=True)
url = models.SlugField(max_length=130, unique=True)
draft = models.BooleanField('Draft', default=False)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('movie_detail', kwargs={'slug': self.url})
def get_review(self):
return self.reviews_set.filter(parent__isnull=True)
class Meta:
verbose_name = 'Movie'
verbose_name_plural = 'Movies'
@ admin.register(Movie)
class MovieAdmin(admin.ModelAdmin):
"""Movie"""
list_display = ('title', 'category', 'url', 'draft')
list_filter = ('category', 'year' )
search_fields = ('title', 'category__name')
inlines = [MovieShotsInLine,ReviewInLine]
save_on_top = True save_as
= True
list_editable = ('draft',)
readonly_fields = ('get_image',)
fieldsets = (
(None, {
"fields": (("title", "tagline")),)
}),
(None, {
"fields": ("descriptions", ("poster", 'get_image'))
} ),
(None, {
"fields": (("year", "world_premiere", "country")),)
}),
("Actors", {
"classes": ("collapse",),
"fields": (("actors", "directors", "genres", "category")),)
}),
(None, {
"fields": (("budget","fees_in_usa", "fees_in_world")),)
}),
("Options", {
"fields": (("url", "draft")),)
}),
)
def get_image(self, obj):
if obj.poster:
return mark_safe(f'
else:
return 'no photo'
get_image.short_description = 'Poster'
Answer the question
In order to leave comments, you need to log in
With the help of inheritance from ModelAdmin, django allows you to declaratively customize the admin interface. In this case, django finds the string "get_image" in readonly_fields and follows a predefined algorithm - it checks if the model has such a field. If not, is there a function defined with that name that takes only one argument. If not (our case), then it is checked whether the given subclass of ModelAdmin has a method with the same name and takes 2 arguments. If there is such a method, then it is called and the current object is passed as the second argument. So the answer to your question is that django calls this method and passes obj.
list_display documentation. Readonly_fields - work exactly the same
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question