保存方法很多
我有一个名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否使用 django-admin 来保存模型和标签?直到模型的保存后信号发出后,多对多字段才会保存在那里。在这种情况下,您可以做的是覆盖管理类 save_model 方法。例如:
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.:
您没有向卡添加任何标签。在保存卡片之前,您无法添加多对多关系,并且在
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 toself.tags.all()
for them to be set.