python get()返回了多个Post --它返回了2

tvokkenx  于 2023-01-29  发布在  Python
关注(0)|答案(1)|浏览(178)

我在django的问题。我创建网上商店网站,我添加产品部分,我的产品上市(html)。我添加我的产品从管理网站(models.py)。当我想添加到产品,我给这样的错误:get()返回了不止一个Post--它返回了2个!
这是我的密码

    • 查看次数. 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
    • 型号. 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

我必须使用coinbase网关支付。我想当用户去coinbase支付的产品标题(每个产品标题)设置在coinbase标题和...但我有这样的错误,当我想添加更多的产品
你能帮帮我吗?

3ks5zfa0

3ks5zfa01#

models = Post.objects.get()

这个方法是get()模型中的一个对象。如果你不使用任何参数,那么它会尝试获取QuerySet中的所有对象。如果有多个对象(或者没有),那么它会抛出一个错误。而这正在发生,因为我可以假设你的数据库中有两个Post对象。
您需要传递如下参数:

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

相关问题