Hi all,
New to Django, I've scoured the Web to see if there's an answer to this particular problem - wondering if anyone could assist:
I have two models related many-to-many through a third, e.g.
class Site(models.Model):
id = models.PositiveSmallIntegerField(primary_key=True)
name = models.CharField(max_length=80)
items = models.ManyToManyField(Item, through="Allocation")
class Item(models.Model):
name = models.CharField(max_length=80)
class Allocation(models.Model):
site = models.ForeignKey(Site)
item = models.ForeignKey(Item)
qty = models.PositiveSmallIntegerField()
and I'm trying to make a view/form for Allocation (for one particular Site - it takes the site ID as an argument to the view).
I've managed to create an inline formset with these lines in my view:
def allocations_page(request, site_id):
site = Site.objects.get(id=site_id)
AllocationSubForm = inlineformset_factory(Site, Site.items.through, form=AllocationForm)
aform = AllocationSubForm()
return render(request, 'allocations.html', { 'subform': aform})
Which seems to work, in that it it shows 3 blank rows - each row prompts the user to select an Item from a dropdown and fill the qty in.
However, rather than having this scenario, I would much prefer to have a row for every Item in the Item table, regardless of whether an Allocation exists for it (show a qty of blank or 0 if there's no entry in Allocation yet), and simply have the name attribute as a label to the "qty" field. In essence, Item left joined to Site through Allocation.
Is this easily achievable?
Thanks,
Miles