简体   繁体   中英

How to print data in template django of a diffrent table joined by foreign key?

Hello Everyone i have Two model first one is as following:

class Item(models.Model):
    title = models.CharField(max_length=100)
    price = models.FloatField()
    bargainprice = models.FloatField(default=0)
    discount_price = models.FloatField(blank=True, null=True)
    category = models.CharField(choices=CATEGORY_CHOICES, max_length=2)
    label = models.CharField(choices=LABEL_CHOICES, max_length=1)
    slug = models.SlugField()
    description = models.TextField()
    image = models.ImageField()

and i am getting this model data using the following view:

class ItemDetailView(DetailView):
    model = Item
    template_name = "product.html"

and in product.html i am accessing Item objects like this:

    <span class="mr-1">
          <del>₹ {{ object.price }}</del>
        </span>
        <span>₹ {{ object.discount_price }}</span>

        {% else %}
        <span> ₹ <span id="pp">{{ object.price }}</span></span>

and so on..

everything working fine up here. but problem arises when i created the following model:

class BargainModel(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL,
                         on_delete=models.CASCADE)
    itemId = models.IntegerField()
    bprice = models.FloatField()

i joined this with foreign key as mentioned. **what i want to do is print the " bprice " in the product.html of the same user but i am not able to do it ** can anyone help me with this i am new to Django. Thanks in advance

in this case you need to import User like

from django.contrib.auth.models import User

class BargainModel(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    itemId = models.ForeignKey(Item, on_delete=models.CASCADE)
    bprice = models.FloatField()

in product.html you can call the model of BargainModel it also contains the Item with user

It is better to work with a ForeignKey since this will guarantee referential integrity. You thus shoudl define the BargainModel as:

from django.conf import settings

class Bargain(models.Model):
    user = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE
    )
    item = models.ForeignKey(
        Item,
        on_delete=models.CASCADE
    )
    bprice = models.FloatField()
    
    class Meta:
        constraints = [
            models.UniqueConstraint(fields=['item', 'user'], name='unique_user_item')
        ]

In the DetailView , we can then look if there is a Bargain record for the given item and user with:

class ItemDetailView(DetailView):
    model = Item
    template_name = "product.html"

    def get_bargain(self):
        if self.request.user.is_authenticated():
            return Bargain.objects.filter(item=self.object, user=request.user).first()

Then you can render this with:

{{ view.get_bargain.bprice }}

if there is a related Bargain , then it will show the corresponding bprice .


Note : Models normally have no Model suffix. Therefore it might be better to rename BargainModel to Bargain .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM