45👍
✅
You don’t actually have a circular reference; the issue is that, at the time you define Album, you haven’t defined Image yet. You can fix that by using a string instead:
class Album(models.model):
thumb = models.ForeignKey('Image', null=True, blank=True)
However, in this case, you might want to use a OneToOneField instead of a foreign key. (Note that you’ll still have to use the trick with the string, though).
13👍
Use quotes to force a lazy reference:
models.ForeignKey('Image', null=True, blank=True)
Also, ForeignKey.related_name is your friend (avoids back-reference name clashes).
- [Django]-Allowing RabbitMQ-Server Connections
- [Django]-Substring in a django template?
- [Django]-How to pass django rest framework response to html?
0👍
This is old but anyway, i’d like to say that I don’t see a reason for attribute album in model Image. In my opinion, it is not really needed.
- [Django]-Rendering a value as text instead of field inside a Django Form
- [Django]-Cannot access django app through ip address while accessing it through localhost
- [Django]-Django 1.7 – App 'your_app_name' does not have migrations
Source:stackexchange.com