Search code examples
pythondjangogenericscoinbase-api

get() returned more than one Post -- it returned 2


I got problem in django. Im creating online shop website and I add products section where my products listed(html). I add my products from admin site (models.py). When I want to add to products i give error like this : get() returned more than one Post -- it returned 2!

These are my codes :

views.py

class PostDetail(generic.DetailView):
    model = Post
    template_name = "shop-single.html"

    def get_context_data(self, **kwargs):
        models = Post.objects.get()
        context = super().get_context_data(**kwargs)
        client = Client(api_key = settings.COINBASE_COMMERCE_API_KEY)
        domain_url = "https://www.nitroshop.store/"
        product = {"name" : f'{models.title}' , 'description': f'{models.subject}' ,  "local_price" : {'amount' : f'{models.product_price}' , "currency" : "USD"} , "pricing_type" : "fixed_price" , "redirect_url" : domain_url + "NXnUijYpLIPy4xz4isztwkwAqSXOK89q3GEu5DreA3Ilkde2e93em8TUe99oRz64UWWBw9gEiiZrg60GMu3ow" , "cancel_url" : domain_url + "products"}
        charge = client.charge.create(**product)
        context['charge'] = charge
        return context

models.py

from django.db import models
from django.contrib.auth.models import User

# Create your models here.

STATUS = (
    (0 , "Draft"),
    (1 , "Publish")
)

class Post(models.Model):
    title = models.CharField(max_length = 200 , unique = True)
    slug = models.SlugField(max_length = 200 , unique = True)
    author = models.ForeignKey(User , on_delete = models.CASCADE , related_name = "shop_posts")
    updated_on = models.DateTimeField(auto_now = True)
    subject = models.CharField(max_length = 200 , default = "We offer you pay with Tether or Litecoin")
    caption = models.TextField()
    product_brand = models.CharField(max_length = 200 , default = "Add parametr")
    product_price = models.CharField(max_length = 200 , default = "Add parametr")
    opt = models.TextField(default = "Add parametr")
    image = models.ImageField(upload_to = "images/" , default = "None")
    created_on = models.DateTimeField(auto_now_add = True)
    status = models.IntegerField(choices = STATUS , default = 0)

    class Meta:
        ordering = ["-created_on"]

    def __str__(self):
        return self.title

I must be use coinbase gateway for payment. I want when user go to coinbase payment the title of product(each product title) set on coinbase title and ... But I have error like that when i want add more products

Would you please help me ?


Solution

  • models = Post.objects.get()
    

    This method is to get() a single object from model. If you don't apply any parameters, then it tries to get all objects in QuerySet. If there is more than one (or None), then it will throw an error. And this is happening, because I can assume you have two Post objects in your database.

    You need to pass parameter like:

    models = Post.objects.get(id=some_id)