顧客のすべてのリスティング(広告)をリストするテンプレート/レポートを生成しようとしています。ほとんどすべてがListingsテーブルのIDに関連しています。顧客は多くのリスティングを持つことができ、リスティングは1つのリスティングタイプのみを持ち、リスティングは多くのイメージを持つことができます。私はview.py
がうんざりしていることを知っています - 理想的には、私はテンプレートに最小限の量のデータを送信したいと思います。そこで、特定の顧客のリスティングに関連するリスティング、画像、リスティングタイプ(1,2,3)のデータのみを送信したいと思います。私はクエリーセットと闘い、文脈を構築しています。現在リストされているものより多くのオブジェクトをコンテキストに追加する必要があると確信しています。django queryset - ビュー内のクエリーセットに関するアドバイスが必要です - フィルターと関係を設定する
テンプレートにデータを取得すると仮定していますが、行ごとにテーブルを作成し、異なるリストタイプを処理するためにテンプレートにif/thenを追加する必要があります。あなたが簡単な方法を知っていれば教えてください。
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
なし - あなたの質問から、私が元の投稿に掲載されていなかったことがわかりました。 – BillB1951