byrdr
byrdr

Reputation: 5487

Cannot assign must be a instance Django

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)

Upvotes: 3

Views: 15709

Answers (1)

MRHwick
MRHwick

Reputation: 1242

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.

Upvotes: 5

Related Questions