ITTONE Création des applications Web

Displaying relationship data inside Django templates

I followed the process of creating multi user type from this tutorial

class User(AbstractUser):
    is_owner = models.BooleanField(default=False)
    is_driver = models.BooleanField(default=False)

class Owner(models.Model):
    user = models.OneToOneField(get_user_model(), on_delete=models.CASCADE, primary_key=True)

I have a driver and owner profile with field user which has a OneToOneField relationship with the user model

class OwnerProfile(models.Model):

    ]
    user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, null=True, related_name="owner_profile")
    phone = models.CharField(max_length=15, blank=True, null=True)
    bio = models.TextField(blank=True, null=True)


class DriverProfile(models.Model):

    ]
    user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, null=True, related_name="owner_profile")
    phone = models.CharField(max_length=15, blank=True, null=True)
    bio = models.TextField(blank=True, null=True)

Owners can create Cars

class Car(models.Model):

    car_owner = models.ForeignKey(settings.AUTH_USER_MODEL, related_name="car_owner", on_delete=models.CASCADE)
    car_make = models.CharField(max_length=20, blank=True, null=True)
    …

While drivers can create CarRents

class CarRent(models.Model):


    car = models.ForeignKey(Car, related_name="rented_car", on_delete=models.CASCADE)
    driver = models.ForeignKey(settings.AUTH_USER_MODEL, related_name="driver_renting", on_delete=models.CASCADE)
    start_date = models.DateTimeField(blank=True, null=True)
    …

I have a detailView

class ActiveRent(DetailView):
    model = CarRent
    template_name="app/ActiveRent_detail.html"

in my template i tried to access data in the Owner profile using each of the following {{object.car_owner.owner_profile.phone}, {{object.car.ownerProfile.phone}}, {{object.ownerProfile.phone}} but nothing is working.

Thanks for your help in advance

Product of the Month September 2016

Source link

Leave a Comment

Your email address will not be published. Required fields are marked *