Hola,

I am trying to get two different "extras" to work.

I have a form with an inline_formset, which I would like to be able to
add multiple formsets of on the fly.

Each of those formsets has another FK to a model with a very large
dataset, so I would also like to implement some sort of
autocomplete/ajax/select2/typeahead solution to prevent an unusably
large select

Here is my basic set up.

models.py
----------------
class PartNumber(models.Model):
    name = models.CharField("Description", max_length=100)
    supplier_part_number = models.CharField(max_length=30,
unique=True, blank=True, null=True)

class PurchaseOrder(models.Model):
    po_number = models.CharField('PO number', max_length=10, unique=True)
    ordered_date = models.DateField(default=today)

class PurchaseOrderPart(models.Model):
    part_number = models.ForeignKey(PartNumber, related_name='purchases')
    po_number = models.ForeignKey(PurchaseOrder, related_name='partslist')
    delivery_date = models.DateField(null=True, blank=True)
    qty_ordered = models.IntegerField('Quantity
ordered',validators=[MinValueValidator(1)])
    cost = models.DecimalField('Unit Cost',
max_digits=10,decimal_places=2,blank=True,null=True)


forms.py
-------------

class PurchaseOrderPartForm(forms.ModelForm):
    class Meta:
      fields = ('part_numbers', 'delivery_date', 'qty_ordered', 'cost')
      model = PurchaseOrderPart
      widgets={
        'part_numbers': forms.Select(attrs={'class':'form-control'}),
        'delivery_date': CalendarWidget(attrs={'class':'input-append
form-control'}),
        'qty_ordered': forms.NumberInput(attrs={'class':'form-control'}),
        'cost': forms.NumberInput(attrs={'class':'form-control'}),
        }

POPartFormset = inlineformset_factory(PurchaseOrder,
PurchaseOrderPart, form=PurchaseOrderPartForm, extra=1,
can_delete=True)


I have successfully implemented jquery.formset.js (
https://github.com/elo80ka/django-dynamic-formset ) to create "add"
and "remove" buttons - this allows for a variable number of
PurchaseOrderParts on a PurchaseOrder.


When I then implemented django-select2 I was mostly successful, except
for one problem - the way that django-select2 implements field is by
adding some js on render.

Since the jquery.formset.js was creating forms on the fly, any
formsets added via it's add button did not have the required
django-select2 container, and hence had no widget at all.

jquery.formset.js has an "added" function that allows for something
like this, but then I would need to glue it all together and it
doesn't seem clean enough.

I had previously looked at typeahead, django-ajax-selects and
django-autocomplete-light but struggled to implement them. I will
likely go back to try again.

Surely I'm not the first person that's had this need - can anyone
offer tips on a good or better solution?

cheers
L.



-- 
The idea is that a beautiful image is frameable. Everything you need
to see is there: It’s everything you want, and it’s very pleasing
because there’s no extra information that you don’t get to see.
Everything’s in a nice package for you. But sublime art is
unframeable: It’s an image or idea that implies that there’s a bigger
image or idea that you can’t see: You’re only getting to look at a
fraction of it, and in that way it’s both beautiful and scary, because
it’s reminding you that there’s more that you don’t have access to.
It’s now sort of left the piece itself and it’s become your own
invention, so it’s personal as well as being scary as well as being
beautiful, which is what I really like about art like that.
-----------------------------------------------------------------------------------------------------------
Adventure Time http://theholenearthecenteroftheworld.com/

-- 
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/CAGBeqiONWsyKP8xymqCV1Y26kqV7ikA9sDxOaqfciG_LNEw0Sg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to