I have a model of categories with a title
class Category(models.Model):
user = models.ForeignKey(user, on_delete=CASCADE)
title = models.CharField(max_length = 20)
I have another model with many to many field of categories
class Product(models.Model):
user = models.ForeignKey(user, on_delete=CASCADE)
Category = models.ManyToManyField(Category)
title = models.CharField(max_length = 20)
Both my models got a user foreign key.
I created a product form using django modelforms.
class ProductForm(forms.ModelForm):
class Meta:
model = Product
fields = '__all__'
The issue I face is that I get the category of another user also. how to show only categories of that particular user? so that one user won't see another user's category
CodePudding user response:
=========== views.py =============
def HomeView(request):
form = ProductForm(user_id=request.user.id)
context = {
'form':form
}
return
render(request,'index.html',context)
========= form.py ===============
from django import forms
from .models import *
class ProductForm(forms.ModelForm):
def __init__(self, user_id, *args, **kwargs):
super(ProductForm, self).__init__(*args, **kwargs)
if user_id is not None:
self.fields['category'].widget = forms.Select(choices = Category.objects.filter(user=user_id).values_list('title','title'))
else:
self.fields['category'].widget = forms.Select(choices = Category.objects.all().values_list('title','title'))
class Meta:
model = Product
fields = '__all__'
exclude = ['user']
=============== models.py ===============
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class Category(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField(max_length = 20)
def __str__(self):
return self.title
class Product(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE,)
category = models.ForeignKey(Category, on_delete=models.CASCADE)
title = models.CharField(max_length = 20)
def __str__(self):
return self.title
==== Output when user not logged in the display all categories ====
==== Output when user one logged in the display category1,category2 ====
==== Output when user two logged in the display category3,category4 ====
======== admin panel category model ===========
CodePudding user response:
================================================
with MultiSelect choices
================================================
======= form.py ========
from django import forms
from .models import *
class ProductForm(forms.ModelForm):
def __init__(self, user_id, *args, **kwargs):
super(ProductForm, self).__init__(*args, **kwargs)
if user_id is not None:
self.fields['category'].widget = forms.SelectMultiple(choices = Category.objects.filter(user=user_id).values_list('title','title'))
else:
self.fields['category'].widget = forms.SelectMultiple(choices = Category.objects.all().values_list('title','title'))
class Meta:
model = Product
fields = '__all__'
exclude = ['user']
======= views.py =========
from django.shortcuts import render
from .form import *
def HomeView(request):
form = ProductForm(user_id=request.user.id)
context = {'form':form}
return render(request,'index.html',context)
========== html ===========
{% extends "base.html" %}
{% block title %}
Index | Page
{% endblock title %}
{% block body %}
<h1>Current user : {{request.user}}</h1>
<hr>
<form action="" method="POST">
{% csrf_token %}
{{form.as_p}}
<button type="submit">submit</button>
</form>
{% endblock body %}
======== modals.py ==========
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class Category(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField(max_length = 20)
def __str__(self):
return self.title
class Product(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE,)
category = models.ManyToManyField(Category)
title = models.CharField(max_length = 20)
def __str__(self):
return self.title
=========== screenshot ouput ===========