0

I'm using django-crispy-forms and would like to use autocomplete-light but can't get it going. I need users to be able to create a new facility if the one they want doesn't exist. I just have no idea how to use autocomplete-light and I've been struggling for days. Can someone please point me in the right direction??

models.py

class CollectionFacility(TimeStampedModel):
    """
    Data collection facility.
    """

    facility_name = models.CharField(max_length=256, blank=False)
    address_line1 = models.CharField("Address line 1", max_length=45)
    address_line2 = models.CharField("Address line 2", max_length=45, blank=True)
    country = models.CharField(max_length=50, blank=False)
    state_province = models.CharField(max_length=100, blank=True)
    city = models.CharField(max_length=100, blank=False)
    postal_code = models.CharField("Postal Code", max_length=20, blank=True)
    facility_contact = models.ForeignKey('FacilityContact', related_name='collection_facilities', null=True, blank=True)

    def __unicode__(self):
        return "%s, %s" %  (self.facility_name, self.country)

    class Meta:
        ordering = ['country', 'facility_name', 'city', 'state_province']
        verbose_name = "Collection Facility"
        verbose_name_plural = "Collection Facilities"

class FacilityContact(TimeStampedModel):
    TITLES = (
        ('Mrs.', 'Mrs.'),
        ('Ms.', 'Ms.'),
        ('Mr.', 'Mr.'),
        ('Dr.', 'Dr.'),
    )

    first_name = models.CharField(max_length=256, blank=False)
    middle_initial = models.CharField(max_length=4, blank=True)
    last_name = models.CharField(max_length=256, blank=False)
    title = models.CharField(max_length=4, choices=TITLES, blank=True)
    email = models.EmailField(blank=False)

    def __unicode__(self):
        return "%s, %s" %  (self.last_name, self.first_name)

    class Meta:
        ordering = ['last_name', 'first_name']
        verbose_name = "Facility Contact"
        verbose_name_plural = "Facility Contacts" 

forms.py

class FacilityForm(autocomplete_light.ModelForm):
    class Meta:
        model = CollectionFacility

views.py

facility_form = FacilityForm()
# pass it in the context to template
....

template.html

{% crispy facility_form %}
steph
  • 701
  • 2
  • 10
  • 30
  • Does django-autocomplete-light work when you're not doing crispy forms? Did you try with a basic example (like in the docs of django-autocomplete-light)? – SaeX Mar 31 '15 at 07:39

1 Answers1

0

Did you check the non_admin_add_another example app ?

Docs about that one have not yet been ported to v2 which mean the code in the docs might not work. However note that autocomplete_light.example_apps.non_admin_add_another should work.

I recommend you start fiddling with that example directly in autocomplete_light's test_project, see: http://django-autocomplete-light.readthedocs.org/en/stable-2.x.x/demo.html

jpic
  • 32,891
  • 5
  • 112
  • 113
  • I tried downloading the example app, but all the static files returned 404. it didn't seem like it was working. – Adam Starrh Sep 30 '15 at 05:58
  • 1
    I tried both examples and fixed one of them: https://github.com/yourlabs/django-autocomplete-light/commit/e8763164c310ae03ae716312b64121f8ff0e5139 Both work now. – jpic Sep 30 '15 at 12:26