not appears the green plus for add another item

10 views
Skip to first unread message

Carlos Rocha

unread,
Oct 9, 2014, 6:32:39 PM10/9/14
to django-s...@googlegroups.com
Hi, people i have read all entries talk the green plus for add new item but no working for me

1. i use django1.6.7 and django selectable 0.8
2. working autocomplete but de green plus does not appear :(

3. my files

first: models.py

@python_2_unicode_compatible
class Farmer(models.Model):
    name = models.CharField(max_length=250, null=True, blank=True)
    ...
    def __str__(self):
        return u'%s' % (self.name)

@python_2_unicode_compatible
class Farms(models.Model):
    name_farmer = models.ForeignKey('Farmer')
    farm = models.CharField("max_length=150,null=True,blank=True)

second: lookups.py

class FarmersLookup(ModelLookup):
    model = Farmer
    search_fields = ('name__icontains', )

registry.register(FarmersLookup)

thirds: forms.py

class FarmerAdminForm(forms.ModelForm):
    name_farmer = selectable.AutoCompleteSelectField(lookup_class=FarmersLookup, allow_new=True)

    class Meta(object):
        model = Farms
        widgets = {
            'name_farmer': selectable.AutoCompleteSelectWidget(lookup_class=FarmersLookup),
        }
        #exclude = ('name_farmer', )

    def __init__(self, *args, **kwargs):
        super(FarmerAdminForm, self).__init__(*args, **kwargs)
        if self.instance and self.instance.pk and self.instance.name_farmer:
            self.initial['name_farmer'] = self.instance.name_farmer.pk

    def save(self, *args, **kwargs):
        name_farmer = self.cleaned_data['name_farmer']
        if name_farmer and not name_farmer.pk:
            name_farmer = Farmer.objects.create_item(name=name_farmer.name,)
        self.instance.name_farmer = name_farmer
        return super(FarmerAdminForm, self).save(*args, **kwargs)

fours: admin.py

class FarmsInline(admin.StackedInline):
    model = Farms
    form = FarmerAdminForm
    extra = 1
    max_num = 1
    min_num = 1

to notice something is wrong?

thank any idea! :)

Cheers

Mark Lavin

unread,
Oct 10, 2014, 9:47:13 AM10/10/14
to django-s...@googlegroups.com
The admin adds the green plus icon and related functionality to the ModelChoice and ModelMultipleChoiceField fields on the form. To make this work with django-selectable you need to override the widget and not the field. In this case name_farmer is no longer a ModelChoiceField and won't get the plus icon. This field override should be removed and only the widget change in the Meta should be there.

--
You received this message because you are subscribed to the Google Groups "django-selectable" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-selecta...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages