I’m using Django auth for my model Dish
. Each user manages many dishes (1-n relationship)
# models.py
class Dish(models.Model):
user_id = models.ForeignKey(User, on_delete=models.CASCADE)
dish_name = models.CharField(max_length=50)
# views.py
class ProfileFormView(LoginRequiredMixin, UpdateView):
login_url = '/accounts/login'
template_name = 'dish.html'
model = Dish
success_url = '/dish/updated'
I want to restrict my UpdateView to only dish’s owner (For example: user 1 owns dish 1 and 2; user 2 owns dish 3 and 4. user 1 can edit dish 1, but cannot edit dish 3 or 4)
Assuming that user manages other models (for example: menu, ratings, etc), and they all need to be restricted to their owners. What is the proper way to achieve this?
Thank you very much!