I have the following models:
class Organization(models.Model):
name = models.CharField(max_length=255)
owner = models.OneToOneField(User, on_delete=models.CASCADE)
def __str__(self):
return f"{self.name}"
class Category(models.Model):
name = models.CharField(max_length=255)
organization = models.ForeignKey(Organization, on_delete=models.CASCADE, related_name='categories')
class Meta:
verbose_name = "category"
verbose_name_plural = "categories"
order_with_respect_to = 'organization'
def __str__(self):
return self.name
class Product(models.Model):
brand = models.CharField(max_length=255, blank=True, null=True)
name = models.CharField(max_length=255)
code = models.CharField(max_length=255)
sale_price = models.DecimalField(decimal_places=2, max_digits=12)
stock = models.DecimalField(decimal_places=2, max_digits=12)
organization = models.ForeignKey(Organization, on_delete=models.CASCADE, related_name='products')
category = models.ForeignKey(Category, on_delete=models.CASCADE, related_name='producs', null=True, blank=True)
class Meta:
order_with_respect_to = 'organization'
I want an inline formset where the user can add products, but I want to show only the categories that are registered to the current user's organization(accessible by request.user.organization
)
Right now, this is how the formset is set up:
def addProducts(request):
ProductFormSet = inlineformset_factory(Organization, Product, exclude=('organization',), extra=5)
organization = request.user.organization
if request.method == "POST":
formset = ProductFormSet(request.POST,
instance=organization, queryset=Product.objects.none())
else:
formset = ProductFormSet(
instance=organization, queryset=Product.objects.none())
context = {"forms": formset}
return render(request, "products/product_form.html", context=context)
But, as you can probably guess, the select field is showing all categories, not just the one's that have the same organization. How do I pass Category.objects.filter(organization=organization)
to the select field?
Here's how I fixed it:
Wrap the ProductForm in a function:
def productform_factory(organization):
class ProductForm(ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields["category"].queryset = Category.objects.filter(
organization=organization)
class Meta:
model = Product
fields = ['brand', 'name', 'code', 'sale_price', 'stock', 'category']
return ProductForm
Change the view code to this:
organization = request.user.organization
ProductFormSet = inlineformset_factory(Organization, Product, form=productform_factory(organization), extra=4)
if request.method == "POST":
formset = ProductFormSet(request.POST,
queryset=Product.objects.none())
else:
formset = ProductFormSet(
queryset=Product.objects.none())
context = {
# 'form': form,
'title': 'Add Product',
'formset': formset
}
return render(request, "products/product_form.html", context=context)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.