我想從我這裏Django應用兩種模式訪問的信息是我的模型:的Python正從兩個模型在Django
class Events(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField(max_length=50, blank=True, default='')
description = models.CharField(max_length=500, blank=True, default='')
date = models.CharField(max_length=200, blank=True, null=True)
time = models.CharField(max_length=200, blank=True, null=True)
price = models.CharField(max_length=240, blank=True, default='')
active = models.CharField(max_length=1, blank=True, default='d')
seats = models.CharField(max_length=200, blank=True, default='')
alcohol_choice = models.CharField(max_length=1, blank=True, default='n')
starter = models.CharField(max_length=350, blank=True, default='')
main_menu = models.CharField(max_length=350, blank=True, default='')
dessert = models.CharField(max_length=350, blank=True, default='')
notes = models.CharField(max_length=350, blank=True, default='')
class Meta:
app_label = 'events'
class GalleryImagesEvents(models.Model):
event = models.ForeignKey(Events, on_delete=models.CASCADE)
file = ResizedImageField(size=[1200, 800], quality=75, upload_to="events/static/galleryimages")
file_thumbnail = ResizedImageField(size=[300, 300], crop=['middle', 'center'], upload_to="events/static/galleryimages/thumbnails")
order = models.TextField(default='', blank=True)
user = models.ForeignKey(User, on_delete=models.CASCADE)
created_date = models.DateTimeField(auto_now_add=True)
modified_date = models.DateTimeField(auto_now=True)
class Meta:
ordering = ['order']
我可以有多個活動的一個用戶,每一個事件可以有多個畫廊圖像。
我想要做的是從用戶獲取所有事件,並且只能從附加到事件的galleryimagesevent模型獲取事件的一個圖像。
@csrf_exempt
@login_required(login_url="/login/")
def EventsView(request):
user = User.objects.get(pk=request.user.id)
events_list_data = Events.objects.filter(user_id=user).order_by('-date')[:10]
events_list_images = GalleryImagesEvents.objects.filter(event_id=events_list_data)
return render(request, 'events/view.html', {"events_list_data":events_list_data, 'events_list_images': events_list_images})
我知道類是用於等等但我仍然學習如此裸露與我的編碼。 :)
沒有得到這一點。如何連接它們。
{% for event_list in events_list_data %}
<div class="panel-body event-box">
<div class="col-md-4 no-padding">
<img src="{{ events_list_images.file_thumbnail.url }}" class="img-responsive img-events-thumb">
</div>
<div class="col-md-6 event-inner-box">
<div>
<h4><b>{{ event_list.title }}</b> <span class="event-small">
{% if event_list.active == 'r' %} Not approved yet! {% endif %}
{% if event_list.active == 'd' %} Approved but deactivated! {% endif %}
{% if event_list.active == 'a' %} Approved & Active! {% endif %}
{% if event_list.active == 'e' %} Expired! {% endif %}
</span></h4>
<p>{{ event_list.description|slice:":100" }}...</p>
<br>
<button class="btn btn-primary btn-small" type="submit">Deactivate</button>
<button class="btn btn-primary btn-small" type="submit">Preview</button>
<button class="btn btn-primary btn-small" type="submit"><span class="fa fa-pencil"></span></button>
</div>
</div>
<div class="col-md-2 event-inner-box-date">
<div>
<p></p>
<h3><span class="dateShow dateformat_{{ event_list.id }}"></span><br><span class="dayformat_{{ event_list.id }}"></span></h3>
</div>
<script>
var dateMonthAsWord = moment('{{ event_list.date }}').format('DD');
var dateMonthAsWordMonth = moment('{{ event_list.date }}').format('MMM');
$('.dateformat_{{ event_list.id }}').html(dateMonthAsWordMonth);
$('.dayformat_{{ event_list.id }}').html(dateMonthAsWord);
</script>
</div>
<div class="clearfix"></div>
</div>
{% endfor %}
以上是我想如何顯示我的活動。
TA!
你想獲得一個隨機的或(在某種程度上),具體從圖像畫廊的圖像?您可以爲這種邏輯創建一個模型屬性 –
任何隨機圖像...如果我能得到一個特別的圖像,那麼這個圖像真的很重要......說出第一張圖像並不重要。 – Darren