0

There are many related questions on SO about this topic, but they all seem to deal with cases where the element being assigned is actually the instance.id or a string with the instance name.

I have two models, one called Photo, the other called Material.

class Photo(models.Model):
     mat = models.ForeignKey(
        Material, related_name='photos',null=True, blank=True) 

In the shell, I get a Photo object, and I create a Material object.

[in : 1]p = Photo.objects.all()[0] # get the first
[in : 2]m, bool = Material.objects.get_or_create(id=1) # get_or_create returns a tuple, take the first

[in : 3]p
[out:1] <Photo: Photo object>
[in : 4]m
[out:2] <Material:Material object>

[in : 5]p.mat = m

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-60-836d23304359> in <module>()
----> 1 p.mat = m

/home/rootadmin/opensurfaces/venv/local/lib/python2.7/site-packages/django/db/models/fields/related.pyc in __set__(self, instance, value)
    595                     instance._meta.object_name,
    596                     self.field.name,
--> 597                     self.field.rel.to._meta.object_name,
    598                 )
    599             )

ValueError: Cannot assign "<Material: Material object>": "Photo.mat" must be a "Material" instance.

As far as I can tell, m is a Material instance. What is going wrong?

3 Answers 3

1

This error raise usually when you trying to assign a instance.id instead of instance.
In the django, you are need to saaign a instance to all the ForeignKey fields.
Instance Ex:

mat = Material.objects.all()[0]

Sign up to request clarification or add additional context in comments.

1 Comment

Usually, yes, but regretfully that's not the case here. The m object is truly a Material instance.
0

Can you check how Material model is imported to the file and make sure it is the same model which is the ForeignKey for Photo ? It looks like you have the wrong Material class imported.

5 Comments

Sorry my codeblock wasn't clear enough. I index with [0] on get or create, meaning it returns the actually object instead of the tuple. This can also been seen in input line 4.
didn't notice that. This is format we usually do. return to two variables.
Yea, I probably should have made it more explicit in the question that I index.
Can you check how Material model is imported to the file and make sure it is the same model which is the ForeignKey for Photo ?
Ha, found it, thanks to your. I had double checked the import locations, which were correct, but turns out the created Material instance was declared somewhere else, e.g. I had two difference Material classes, and I tried to assign the wrong one. Thanks!
0

The element m is an actual Material instance. This doesn't mean it is the right Material instance.

In my case, it turned out there were two separate declarations of a class named Material. As a result, I was trying to assign the wrong Material instance.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.