保存方法很多

发布于 2024-08-19 00:54:02 字数 1389 浏览 2 评论 0原文

我有一个名为 Card 的模型,它与 标签。当我保存卡片时,我也想创建一个产品,我 想要有相同的多对多关系来标记。

如何访问实例的标签? self.tags.all() 给出一个空 列表,而如果我保存后检查,该卡实际上有标签。我的 代码如下。作为记录,我使用的是 Django 1.0.5。

class Card(models.Model): 
    product     = models.ForeignKey(Product, editable=False, null=True) 
    name       = models.CharField('name', max_length=50, unique=True, help_text='A short and unique name or title of the object.') 
    identifier = models.SlugField('identifier', unique=True, help_text='A unique identifier constructed from the name of the object. Only change this if you know what it does.', db_index=True) 
    tags       = models.ManyToManyField(Tag, verbose_name='tags', db_index=True) 
    price      = models.DecimalField('price', max_digits=15, decimal_places=2, db_index=True) 
    def add_product(self): 
        product = Product( 
            name = self.name, 
            identifier = self.identifier, 
            price = self.price 
        ) 
        product.save() 
        return product 
    def save(self, *args, **kwargs): 
        # Step 1: Create product 
        if not self.id: 
            self.product = self.add_product() 
        # Step 2: Create Card 
        super(Card, self).save(*args, **kwargs) 
        # Step 3: Copy cards many to many to product 
        # How do I do this? 
        print self.tags.all() # gives an empty list?? 

I've got a model called Card which has a ManyToMany relationship to
Tag. When I save a Card, I'd like to create a Product as well, which I
want to have the same ManyToMany relationship to tag.

How do I access the instance's tags? self.tags.all() gives an empty
list, while if I check after saving, the card actually has tags. My
code is below. For the record, I am using Django 1.0.5.

class Card(models.Model): 
    product     = models.ForeignKey(Product, editable=False, null=True) 
    name       = models.CharField('name', max_length=50, unique=True, help_text='A short and unique name or title of the object.') 
    identifier = models.SlugField('identifier', unique=True, help_text='A unique identifier constructed from the name of the object. Only change this if you know what it does.', db_index=True) 
    tags       = models.ManyToManyField(Tag, verbose_name='tags', db_index=True) 
    price      = models.DecimalField('price', max_digits=15, decimal_places=2, db_index=True) 
    def add_product(self): 
        product = Product( 
            name = self.name, 
            identifier = self.identifier, 
            price = self.price 
        ) 
        product.save() 
        return product 
    def save(self, *args, **kwargs): 
        # Step 1: Create product 
        if not self.id: 
            self.product = self.add_product() 
        # Step 2: Create Card 
        super(Card, self).save(*args, **kwargs) 
        # Step 3: Copy cards many to many to product 
        # How do I do this? 
        print self.tags.all() # gives an empty list?? 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

记忆之渊 2024-08-26 00:54:02

您是否使用 django-admin 来保存模型和标签?直到模型的保存后信号发出后,多对多字段才会保存在那里。在这种情况下,您可以做的是覆盖管理类 save_model 方法。例如:

class CardAdmin(admin.ModelAdmin):

    def save_model(self, request, obj, form, change):
        obj.save()
        form.save_m2m()
        #from this point on the tags are accessible
        print obj.tags.all()

Are you using the django-admin to save the model and tags? The many-to-many fields don't get saved there until after the post-save signal of the model. What you can do in this case is overide the admin classes save_model method. E.g.:

class CardAdmin(admin.ModelAdmin):

    def save_model(self, request, obj, form, change):
        obj.save()
        form.save_m2m()
        #from this point on the tags are accessible
        print obj.tags.all()
情绪失控 2024-08-26 00:54:02

您没有向卡添加任何标签。在保存卡片之前,您无法添加多对多关系,并且在 save 调用和调用 self.tags.all() 之间没有时间让它们添加被设置。

You aren't adding any tags to the Card. You can't add ManyToMany relationships until after you save the Card, and there's no time between the save call and the call to self.tags.all() for them to be set.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文