我有兩個模型與外鍵,一對多的關係,以便我可以在管理員中獲得一箇中繼器模型(圖像)。圖像中繼器工作正常,我的問題是保存在其中一個電影帖子/對象上的字段 - video_stills在所有電影帖子中重複。直放站對象出現在所有的細節視圖 - Django
這裏是我的代碼:
model.py
from __future__ import unicode_literals
from django.db import models
from embed_video.fields import EmbedVideoField
from sorl.thumbnail import ImageField
class Timestamp(models.Model):
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
class Meta:
abstract = True
# Film Model
class Film(Timestamp):
title = models.CharField(max_length=255)
order = models.PositiveIntegerField(default=0, blank=False, null=False)
meta_description = models.TextField('Meta Description', max_length=170,
help_text='Content for description meta tag - Max 170 Characters')
slug = models.SlugField(unique=True)
image = models.ImageField(upload_to='thumb')
video = EmbedVideoField(blank=True)
director = models.CharField(max_length=255,blank=True)
cinematographer = models.CharField(max_length=255,blank=True)
producer = models.CharField(max_length=255,blank=True)
publish = models.BooleanField(default=False)
date_published = models.DateTimeField()
# thumb for admin
def image_thumb(self):
return '<img src="%s" height="200" width="300"/>' % (self.image.url)
image_thumb.short_description = 'Image'
image_thumb.allow_tags = True
# override the admin name + add ordering
class Meta(object):
ordering = ('order',)
verbose_name_plural = "Film Projects"
def __unicode__(self):
return self.title
# helper method
def get_absolute_url(self):
return "/film/%s/" % self.slug
def save(self, *args, **kwargs):
super(Film, self).save(*args, **kwargs)
# Film Stills Image Model
class FilmStillsImage(models.Model):
video_stills = models.FileField(upload_to = 'images')
film = models.ForeignKey(Film)
class Meta(object):
verbose_name_plural = "Film Stills"
views.py
# film single
def film_detail(request, slug):
film = get_object_or_404(Film, slug=slug)
# get all repeater images
film_grabs = FilmStillsImage.objects.all()
try:
next_film = film.get_next_by_date_published()
except Film.DoesNotExist:
next_film = None
try:
previous_film = film.get_previous_by_date_published()
except Film.DoesNotExist:
previous_film = None
return render(request, 'film/film_detail.html', {
'film': film,
'film_grabs': film_grabs,
'next_film': next_film,
'previous_film': previous_film
})
film_detail.html
<div class="section project-stills">
{% if film_grabs %}
<div class="row">
{% for filmstillsimage in film_grabs %}
<div class="grid-item four">
{% thumbnail filmstillsimage.video_stills "600x338" crop="center" as im %}
<img src="{{ im.url }}">
{% endthumbnail %}
</div>
{% if forloop.counter|divisibleby:4 %}
</div>
<div class="row">
{% endif %}
{% endfor %}
</div>
{% else %}
<p>Nothing found.</p>
{% endif %}
</div>
admin.py
class FilmStillsImageAdmin(admin.ModelAdmin):
pass
class FilmStillsImageInline(admin.StackedInline):
model = FilmStillsImage
max_num = 8
extra = 0
class FilmAdmin(SortableAdminMixin, admin.ModelAdmin):
model = Film
list_display = ('title', 'meta_description', 'image_thumb', 'video', 'director', 'cinematographer', 'producer',)
inlines = [FilmStillsImageInline]
prepopulated_fields = {'slug': ('title',)}
我確定它是一個簡單的修復,我有一種感覺,因爲在我的視圖objects.all(),填充它到每個項目。
道歉,如果它的一個愚蠢的問題,並感謝您的任何幫助!
這個不清楚,恐怕。 *電影圖像在哪裏重複? –
對不起,如果我不清楚。相關字段'video_stills'的圖像出現在每個電影項目或帖子(詳細視圖)上,即使它們沒有保存在每個項目上。我希望這是有道理的! – darcemac