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




-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/20234e55-841d-4ec5-9b59-d31f9229dda7%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to