I have product table in my model that is connected to another profile model through using a Foreignkey , when building a form to update a product or add a new one to the product model, the corresponding form field results from django forms results in a multiple choice dropdown with all profiles where I would like to restrict it to the current user only,
here is my model
class Product(models.Model):
name = models.CharField(max_length=100)
price = models.DecimalField(default=0, decimal_places=2, max_digits=6)
category = models.ForeignKey(Category ,on_delete=models.CASCADE )
vendor = models.ForeignKey(Vendor, on_delete=models.CASCADE)
description = models.CharField(max_length=250, default='', blank=True, null=True)
image = models.ImageField(upload_to='uploads/product/')
is_sale = models.BooleanField(default=False)
sale_price = models.DecimalField(default=0, decimal_places=2, max_digits=6)
here is the form
class Product_Form(forms.ModelForm):
name = forms.CharField(label="", widget=forms.TextInput(attrs={'class':'form-control', 'placeholder':' enter your product name'}), required=True , error_messages = {'required':"Please Enter your Name1"})
price = forms.DecimalField(label="", widget=forms.TextInput(attrs={'class':'form-control', 'placeholder':' price'}), required=True , error_messages = {'required':"Please Enter your Name2"})
category = forms.ModelChoiceField(label="category",queryset=Category.objects.all() , required=True, error_messages = {'required':"Please Enter your Name3"})
description = forms.CharField(label="description", widget=forms.TextInput(attrs={'class':'form-control', 'placeholder':' description'}), required=True , error_messages = {'required':"Please Enter your Name4"})
image = forms.ImageField(label="upload product image", required=False, error_messages = {'required':"Please Enter your Name5"})
is_sale = forms.BooleanField(label="is_sale", required=False , error_messages = {'required':"Please Enter your Name6"})
sale_price = forms.DecimalField(label="sale_price", widget=forms.TextInput(attrs={'class':'form-control', 'placeholder':' sale price'}), required=True , error_messages = {'required':"Please Enter your Name7"})
class Meta:
model = Product
fields = ("name", "price", "category", "vendor", "description", "image", "is_sale", "sale_price")
and here is a screen shot from the resulting page with the faulty field with a circle around it
how to make the dropdown has just a single option corresponding to the current profile
