Reputation: 58
There are several models that are related to User model:
class User(AbstractUser):
# Its fields
class Model_1(models.Model)
user = models.OneToOneField('User', on_delete=models.CASCADE)
# other fields
class Model_2(models.Model)
user = models.OneToOneField('User', on_delete=models.CASCADE)
# other fields
class Model_3(models.Model)
user = models.OneToOneField('User', on_delete=models.CASCADE)
# other fields
class Model_4(models.Model)
user = models.OneToOneField('User', on_delete=models.CASCADE)
# other fields
and in the view:
def index(request):
# If the user is related to model one, do something
# If the user is related to model two, do something
# If the user is related to model three, do something
# If the user is related to model four, do something
# Otherwise return to the home page
After the user sends the request to View, how do I determine which of these models it belongs to?
Thank you in advance.
Upvotes: 0
Views: 66
Reputation: 377
Just check for the existence of your model in the user instance
class Model_4(models.Model)
user = models.OneToOneField('User',
related_name='model_4',
on_delete=models.CASCADE)
def index(request):
if request.user.model_4 is not None:
pass # Do something
Upvotes: 1