WDI-SEA / project-4-issues

Open an issue to receive help on project 4 issues
0 stars 0 forks source link

We are running into issues with our relationship models #11

Closed kellylarrea closed 2 years ago

kellylarrea commented 2 years ago

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)

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
    }

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

DoireannJane commented 2 years ago

I think it's your order of imports