2012-04-02 15 views
0

我正在嘗試生成列出客戶所有列表(廣告)的模板/報告。大多數情況都與「列表」表的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)  
+0

什麼ListingType和Listingtype1之間的關係? – okm 2012-04-02 07:47:58

+0

沒有 - 但我從你的問題中注意到我沒有在我的原始文章中包含清單。 – BillB1951 2012-04-02 12:05:56

回答

1

就以lookups with relationsbackwards relationship看看這樣你就可以像鏈條所有型號鏈接:

Image.objects.filter(listings__customer=customer) 

此外,一些offtopic的建議。

一上市便只有一個房源類型

所以,你應該用OneToOneField這裏

你並不需要檢索customer.id,使用customer進行查找。

使用非複數名稱爲您的模型(Listings應該Listing),並避免ListingType1ListingType2複製喜歡你的代碼,使用model inheritance

+0

ilvar - 謝謝,我看到你的觀點,我一直無法理解使用onetoone字段有什麼優勢,或者使用ForeignKey有什麼缺點。我認爲當看模型時,它會使關係更加明顯。非複數評論很好,這也一直困擾着我。 Listingtype1.2.3僅用於發佈目的,實際上不同發音類型的表格是非常不同的。 – BillB1951 2012-04-04 20:24:50

+0

在數據庫FK和OTO字段是相同的,在OTO上只有自動'unique = True'。但是OTO字段具有非常緊湊的後向關係('ListingType.listing'而不是'ListingType.listing_set.all()[0]'),並且更具可讀性(這是最重要的)。在代碼的任何部分,你都應該考慮到其他一些程序員需要使用這些代碼,並且你將無法解釋爲什麼它是這樣的。 – ilvar 2012-04-04 23:04:54

+0

感謝您的回答。這有很大幫助。 – BillB1951 2012-04-05 00:56:41