不能分配必须是一个实例Django [英] Cannot assign must be a instance Django

查看:32
本文介绍了不能分配必须是一个实例Django的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个订单表格,它返回此提交声明:

I have an order form which returns this statement of submit:

Cannot assign "<Annual: 2012>": "Order.annuals" must be a "Catalog" instance.

我对 Django 相当陌生.我知道它需要一个实例而不是它传递的字符串.我将如何解决这个问题?

I'm fairly new to Django. I understand it needs an instance instead of the string it has been passed. How would I go about resolving that?

这是我的观点:

class OrderListCreateView(
  views.LoginRequiredMixin,
  views.SetHeadlineMixin,
  generic.CreateView
     ):
  form_class = forms.OrderListForm
  headline = 'Create'
  model = Order
  template_name = 'ordercreate.html'


  def form_valid(self, form):
    self.object = form.save(commit=False)
    self.object.user = self.request.user
    self.object.save()
    return super(OrderListCreateView, self).form_valid(form)

这是我的表格:

class OrderListForm(forms.ModelForm):

annuals = forms.ModelChoiceField(queryset=Annual.objects.all())
issues = forms.ModelChoiceField(queryset=Issue.objects.all())
articles = forms.ModelChoiceField(queryset=Article.objects.all())

class Meta:
    fields = (
                'annuals',
                'issues',
                'articles',)
    model = models.Order

def __init__(self, *args, **kwargs):
    super(OrderListForm, self).__init__(*args, **kwargs)
    self.helper = FormHelper()
    self.helper.layout = Layout(
                'annuals',
                'issues',
                'articles',
        ButtonHolder(
            Submit('create', 'Create')

        )

    )

这是我的模型:

class Catalog(models.Model):
    products = models.CharField(max_length=200)

    def __unicode__(self):
        return self.products


class Issue(models.Model):
    catalog = models.ForeignKey(Catalog, related_name='issue_products')
    Volume = models.DecimalField(max_digits=3, decimal_places=1)

    def __unicode__(self):
        return unicode(self.Volume)


class Annual(models.Model):
    catalog = models.ForeignKey(Catalog, related_name='annual_products')
    year_id = models.IntegerField(max_length=4)
    start_date = models.CharField(max_length=6)
    end_date = models.CharField(max_length=6)
    def __unicode__(self):
        return unicode(self.year_id)

    #def __unicode__(self):
    #    return unicode(self.id)

class Annual_Issue(models.Model):
    annual_id = models.ForeignKey(Annual, related_name='annual_ids')
    issue_id = models.ForeignKey(Issue, related_name='issues')
    def __unicode__(self):
        return self.annual_id


class Article(models.Model):
    catalog = models.ForeignKey(Catalog, related_name='article_products')
    title = models.CharField(max_length=200)
    abstract = models.TextField(max_length=1000, blank=True)
    full_text = models.TextField(blank=True)
    proquest_link = models.CharField(max_length=200, blank=True, null=True)
    ebsco_link = models.CharField(max_length=200, blank=True, null=True)

    def __unicode__(self):
        return self.title


class Order(models.Model):
    user = models.ForeignKey(User, related_name='who_ordered')
    annuals = models.ForeignKey(Catalog, related_name='annuals_ordered', blank=True, null=True)
    issues = models.ForeignKey(Catalog, related_name='issues_ordered', blank=True, null=True)
    articles = models.ForeignKey(Catalog, related_name='items_ordered', blank=True, null=True)

推荐答案

在您的 Order 模型中,您已经为其他几个模型(Annual、Issue 和 Article)定义了一个 ForeignKey 关系,但是这些关系中的每一个都指向 Catalog模型.当您尝试保存表单创建的 Order 实例时,它已收到这些类型的对象(Annual、Issue 和 Article),但它无法在 Order 模型上定义的字段中存储对这些对象的外键引用.这是因为 Order 上的外键字段要求它们只能包含对 Catalog 对象的引用.

In your Order model, you have defined a ForeignKey relationship for several other models (Annual, Issue, and Article), but each of these relationships points to the Catalog model. When you attempt to save the Order instance created by your form, it has received objects of these types (Annual, Issue, and Article), but it cannot store a foreign-key reference to these objects in the fields defined on the Order model. This is due to the foreign-key fields on the Order demanding that they can only contain a reference to Catalog objects.

如果对于这些外键关系中的每一个,您希望存储这些不同类型的对象之一,则需要更改 Order 模型定义以期望引用这些模型的对象而不是 Catalog 对象.

If, for each of these foreign-key relationships, you wish to store one of these various kinds of objects, you will need to alter your Order model definition to expect references to objects of those models rather than Catalog objects.

简而言之,我建议修改 Order 模型以包含以下关系.这将允许订单对象存储对彼此类型(年度、问题和文章)对象的单个引用.

In brief, I would suggest that the Order model be modified to include the following relationships. This will allow an order object to store a single reference to an object of each other kind (Annual, Issue, and Article).

annuals = models.ForeignKey(Annual, related_name='annuals_ordered', blank=True, null=True)
issues = models.ForeignKey(Issue, related_name='issues_ordered', blank=True, null=True)
articles = models.ForeignKey(Article, related_name='items_ordered', blank=True, null=True)

有关 Django 中外键关系的更多信息,参见此处的参考.

For more information about ForeignKey relationships in Django, see the reference here.

这篇关于不能分配必须是一个实例Django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆