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

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

问题描述

 无法分配< Annual:2012>: Order.annuals必须是Catalog实例。 

我对Django来说相当新鲜。我理解它需要一个实例,而不是它已经传递的字符串。我如何解决这个问题?



这是我的观点:

  class OrderListCreateView(
views.LoginRequiredMixin,
views.SetHeadlineMixin,
generic.CreateView
):
form_class = forms.OrderListForm
headline ='创建'
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()
返回超级(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 =(

'articles',
'articles',)
model = models.Order

def __init __(self,* args,** kwargs):
super(OrderListForm,self).__ init __(* args,** kwargs)
self.helper = FormHelper()
self.helper.layout = Layout(
'annuals' ,
'issue',
'articles',
ButtonHolder(
提交('创建','创建')




这是我的模型:


$ b $
$ = $。

def __unicode __(self):
return self.products

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

def __unicode __(self):
return unicode(sel


class年(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



类文章(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.TextFiel d(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 = 'issue_ordered',blank = True,null = True
articles = models.ForeignKey(Catalog,related_name ='items_ordered',blank = True,null = True)
/ pre>

解决方案

在您的订单模型中,您为其他几种型号定义了一个ForeignKey关系(年度,发行和文章) ,但这些关系中的每一个都指向目录模型。当您尝试保存由表单创建的Order实例时,它已经收到了这些类型的对象(Annual,Issue和Article),但是它不能在Order模型中定义的字段中存储这些对象的外键引用。这是由于订单上的外键字段要求它们只能包含对Catalog对象的引用。



如果对于每个这些外键关系,您希望存储这些各种对象之一,您将需要更改您的订单模型定义,以期望引用这些模型的对象,而不是Catalog对象。



简短的说,我建议修改订单模型以包括以下关系。这将允许订单对象存储对对方的单一引用(年度,发行和文章)。

  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中ForeignKey关系的更多信息,请查看参考这里


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

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

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?

Here is my view:

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)

Here is my 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')

        )

    )

Here is my model:

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)

解决方案

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.

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.

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)

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

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

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