Closed kellylarrea closed 2 years ago
DR
We are trying to create a bookings model and we are not sure how to set the relationship between a pet and a user.
Bookings.py from django.db import models # Create your models here. class Booking(models.Model): # define fields # https://docs.djangoproject.com/en/3.0/ref/models/fields/ start_date = models.DateTimeField() end_date = models.DateTimeField() pets = models.ForeignKey("Pet",on_delete=models.CASCADE) sitter = models.ForeignKey("User",on_delete=models.CASCADE) def __str__(self): return f'{self.start_date}' def as_dict(self): return { 'start_date': self.start_date, 'end_data': self.end_date, } pets.py from django.db import models from django.db.models.fields import related from django.contrib.auth import get_user_model from .booking import Booking from .user import User class Pet(models.Model): name = models.CharField(null =True,max_length=100) pet_owner = models.ForeignKey( get_user_model(), on_delete=models.CASCADE, related_name="pets_owned" ) pet_sitter = models.ForeignKey(User, related_name='client_pet', on_delete=models.CASCADE) booking_pet = models.ManyToManyField( User, through=Booking, through_fields=('pet','user') ) def __str__(self): return self.name def as_dict(self): return { 'name': self.name }
ImportError: cannot import name 'Pet' from partially initialized module 'api.models.pet' (most likely due to a circular import)
Right now we are sure is something to do with how we are import our User. Seems to be going in a circle.
we tried making relationships in the User model and then tried adding relationships in our Pet model
I think it's your order of imports
What stack are you using?
(ex: MERN(mongoose + react), DR(django + react), PEN, etc.)
DR
What's the problem you're trying to solve?
We are trying to create a bookings model and we are not sure how to set the relationship between a pet and a user.
Post any code you think might be relevant (one fenced block per file)
If you see an error message, post it here. If you don't, what unexpected behavior are you seeing?
ImportError: cannot import name 'Pet' from partially initialized module 'api.models.pet' (most likely due to a circular import)
What is your best guess as to the source of the problem?
Right now we are sure is something to do with how we are import our User. Seems to be going in a circle.
What things have you already tried to solve the problem?
we tried making relationships in the User model and then tried adding relationships in our Pet model