我正在嘗試生成列出客戶所有列表(廣告)的模板/報告。大多數情況都與「列表」表的ID相關。一個客戶可以有很多列表,一個列表只會有一個列表類型,而一個列表可以有很多圖片。我知道我的view.py
已經搞亂了 - 理想情況下,我想向模板發送最少量的數據。因此,我只想發送與特定客戶的列表相關的列表,圖片和列表類型(1,2或3)數據。我正在努力處理查詢集,並構建上下文。我相信我必須添加更多的對象上下文比目前列出。django queryset - 在查詢集中查看需要建議 - 過濾器和設置關係
我假設一旦我得到的數據的模板,我將不得不逐行建立一個表,並做一些如果/然後在模板中的東西來處理不同的列表類型。讓我知道如果你知道更簡單的方法。
Models.py
class Customer(models.Model):
name = models.CharField(max_length=20, blank=True)
email = models.CharField(max_length=50, blank=True)
user = models.ForeignKey(User, unique=True)
def __unicode__(self):
return unicode(self.user)
class ListingType(models.Model):
desc = models.CharField(max_length=35, blank=True)
def __unicode__(self):
return self.desc
class Listings(models.Model):
createdate = models.DateTimeField(auto_now_add=True)
price = models.IntegerField(null=True, blank=True)
listing_type = models.ForeignKey(ListingType)
customer = models.ForeignKey(Customer)
class Listingtype1(models.Model):
manufacturer = models.CharField(max_length=35, blank=True)
mfg_no = models.CharField(max_length=35, blank=True)
typespecific1 = models.CharField(max_length=35, blank=True)
typespecific2 = models.CharField(max_length=35, blank=True)
listings = models.ForeignKey(Listings)
class Listingtype2(models.Model):
manufacturer = models.CharField(max_length=35, blank=True)
mfg_no = models.CharField(max_length=35, blank=True)
typespecific1 = models.CharField(max_length=35, blank=True)
typespecific2 = models.CharField(max_length=35, blank=True)
listings = models.ForeignKey(Listings)
class Listingtype3(models.Model):
manufacturer = models.CharField(max_length=35, blank=True)
mfg_no = models.CharField(max_length=35, blank=True)
typespecific1 = models.CharField(max_length=35, blank=True)
typespecific2 = models.CharField(max_length=35, blank=True)
listings = models.ForeignKey(Listings)
class Image(models.Model):
title = models.CharField(max_length=60, blank=True, null=True)
image = models.ImageField(upload_to="images/", blank=True, null=True)
thumbnail = models.ImageField(upload_to="images/", blank=True, null=True)
listings = models.ForeignKey(Listings)
Views.py(工作正在進行中)
def listings_customer(request, user_id):
customer = get_object_or_404(Customer, user=user_id)
cusnum=customer.id
listings = Listings.objects.filter(customer=cusnum)
image = Image.objects.all()
context=Context({
'title': 'Listings',
'customer': customer,
'listings' : listings,
'image' : image,
})
return render_to_response('bsmain/listings.html', context)
什麼ListingType和Listingtype1之間的關係? – okm 2012-04-02 07:47:58
沒有 - 但我從你的問題中注意到我沒有在我的原始文章中包含清單。 – BillB1951 2012-04-02 12:05:56