Closed abhiabhi94 closed 3 years ago
My CustomUser model had a manytomany relationship with a Video model. The video model then had a flag field which would in turn call get_user_model() and lead to either circular import errors or the error listed above depending on the order of the Installed_apps list.
Solution: In CustomUser instead of importing the video model and initiating the field like:
favorite_videos = models.ManyToManyField(Video, blank=True, related_name='favorite_videos')
Don't import the video model and initialize like:
favorite_videos = models.ManyToManyField('api.Video', blank=True, related_name='favorite_videos')
I have tried experimenting inside the testapp
trying to reproduce your issue. You can check the branch fix/circular-import
and tell me what needs to be done.
In short, it now has User
model(which is equivalent to your CustomUser
) inside the user_profile
app.
It acts as a foreign key to Post
model of the post
app. Now, the flag model is associated with this Post
model. As of now, I can't reproduce the issue.
Just to be safe, have you added the location of your user model to settings.AUTH_USER_MODEL
?
Yes, I set settings.AUTH_USER_MODEL to my custom user model.
My user model included these relevant fields:
favorite_artists = models.ManyToManyField(Artist, blank=True, related_name='favorite_artists') blocked_artists = models.ManyToManyField(Artist, blank=True, related_name='blocked_artists') favorite_videos = models.ManyToManyField(Video, blank=True, related_name='favorite_videos') blocked_videos = models.ManyToManyField(Video, blank=True, related_name='blocked_videos')
The video model contains the flag field as well as a separate manytomany field pointing back to the user model via settings.AUTH_USER_MODEL and a foreign key relationship with the artist model.
I tested initializing with get_user_model() on a separate model and ran into the same issue so I don't think the problem is unique to this project.
Can you just upload a minimum reproducible example in the form of a repository somewhere and share that link here? It will help me in seeing all the code, rather than shooting in the dark. As of now, I can't seem to reproduce the issue on my end.
https://github.com/TSolo315/testflag
Ran into the issue making migrations for customUser after adding the many to many fields.
Thanks, I will try to look into this when I have some more time today.
Okay, so I could fix this by make these changes:
settings.AUTH_USER_MODEL
in models.py
get_user_model
over a modular level in utils.py
, use it directly when it is required.Please try to verify if it works at your end too.
Thanks for the report. If you can make these changes in #18, I will be happy to merge.
Also, as a piece of advice, you may probably add a third field non-binary to your gender choices
_Originally posted by @TSolo315 in https://github.com/abhiabhi94/django-flag-app/pull/18#discussion_r589942499_