django-smart-selects icon indicating copy to clipboard operation
django-smart-selects copied to clipboard

Trouble using Smart-Select forms.py

Open mohandeath opened this issue 8 years ago • 17 comments
trafficstars

hey guys ,

i have problem using this with my own app forms.py . it works well in django-admin

Steps to reproduce

my code -> core/models.py

class City(DateTimeAwareModel):
    province = models.ForeignKey(
        verbose_name=_('province'),
        help_text=_('select province of'),
        to='Province',
        related_name='provinces', )
    name = models.CharField(
        verbose_name=_('name'),
        help_text=_('name of the city'),
        max_length=255,
        blank=True
    )
    latitude = models.DecimalField(
        verbose_name=_('latitude'),
        help_text=_('enter latitude of the city'),
        max_digits=9,
        decimal_places=6,
        null=True,
        blank=True
    )
    longitude = models.DecimalField(
        verbose_name=_('longitude'),
        help_text=_('enter longitude of the city'),
        max_digits=9,
        decimal_places=6,
        null=True,
        blank=True
    )

    def __str__(self):
        return '{name}'.format(name=self.name)

    class Meta:
        verbose_name = _('city')
        verbose_name_plural = _('cities')


class Province(DateTimeAwareModel):


    name = models.CharField(
        verbose_name=_('name'),
        help_text=_('name of the province'),
        max_length=255,
        blank=True
    )
    latitude = models.DecimalField(
        verbose_name=_('latitude'),
        help_text=_('enter latitude of the province'),
        max_digits=9,
        decimal_places=6
    )
    longitude = models.DecimalField(
        verbose_name=_('longitude'),
        help_text=_('enter longitude of the province'),
        max_digits=9,
        decimal_places=6
    )

    def __str__(self):
        return '{name}'.format(name=self.name)

    class Meta:
        verbose_name = _('province')
        verbose_name_plural = _('provinces')


class Address(DateTimeAwareModel):
    class Meta:
        verbose_name = _('address')
        verbose_name_plural = _('addresses')

    title = models.CharField(
        verbose_name=_('title of the address'),
        help_text=_('enter the title of the adress'),
        max_length=200,
    )
    user = models.ForeignKey(
        verbose_name=_('user'),
        help_text=_('user of the address'),
        to=UserProfile,
        related_name='addresses'
    )
    province = models.ForeignKey(
        verbose_name=_('province'),
        help_text=_('province in city'),
        to='Province',

    )
    # lib=> smart_select app
    city = ChainedForeignKey(
        verbose_name=_('city'),
        help_text=_('city of the address'),
        to=City,
        related_name='addresses',
        chained_field = "province",
        chained_model_field = "province",
        show_all = False,
        auto_choose = True,
        sort = True,
    )
    text = models.TextField(
        verbose_name=_('text'),
        help_text=_('text of the address')
    )
    first_name = models.CharField(
        verbose_name=_('first name'),
        help_text=_('first name of the transferee'),
        max_length=255
    )
    last_name = models.CharField(
        verbose_name=_('last name'),
        help_text=_('last name of the transferee'),
        max_length=255
    )
    postal_code = models.CharField(
        verbose_name=_('postal code'),
        help_text=_('postal code of the transferee'),
        max_length=10
    )
    phone = models.CharField(
        verbose_name=_('phone'),
        help_text=_('phone number of the transferee'),
        max_length=20,
        validators=[validators.phone_validator]
    )
    cell_phone = models.CharField(
        verbose_name=_('cell phone'),
        help_text=_('cell phone number of the transferee'),
        max_length=20,
        validators=[validators.phone_validator]
    )

    def __str__(self):
        return '{text}'.format(text=self.text)

and core/forms.py :

class AddressForm(forms.ModelForm):
    province = forms.ModelChoiceField(
        queryset=Province.objects.all(),
        empty_label=_('select province'),
        label=_('province'),
    )

    ct = ChainedModelChoiceField(
        queryset=City.objects.all(),
        empty_label=_('select city'),
        label=_('city'),

        to_app_name='core', to_model_name='city', chained_field='province', chained_model_field='province',
        foreign_key_app_name='core', foreign_key_model_name='address', foreign_key_field_name='city',
        show_all=False, auto_choose=False,

    )

    class Meta:
        model = Address
        fields = ['title', 'province', 'ct', 'first_name', 'last_name', 'postal_code', 'cell_phone', 'phone', 'text']
        widgets = {
            'first_name': forms.widgets.TextInput(
                attrs={'placeholder': _('first name of the transferee'), 'class': 'form-control'}),
            'title': forms.widgets.TextInput(
                attrs={'placeholder': _('enter the title of the adress'), 'class': 'form-control'}),
            'last_name': forms.widgets.TextInput(
                attrs={'placeholder': _('last name of the transferee'), 'class': 'form-control'}),
            'postal_code': forms.widgets.TextInput(
                attrs={'placeholder': _('postal code of the transferee'), 'class': 'form-control'}),
            'cell_phone': forms.widgets.TextInput(
                attrs={'placeholder': _('cell phone four account owner'), 'class': 'form-control'}),
            'phone': forms.widgets.TextInput(
                attrs={'placeholder': _('phone number of the transferee'), 'class': 'form-control'}),

        }

Expected behavior

i expect this form to work just fine like admin

Actual behavior

City fields has NO option in view unless if i set show_all = True

hope you guys reply soon . :)

mohandeath avatar Feb 24 '17 13:02 mohandeath

This is with the current git master branch? I assume so since you said the admin was working just fine, I'm just double checking.

Fire up your browser debugging console and see what HTTP requests are happening please. Is the AJAX request firing? What does the AJAX request look like? What does the AJAX response look like?

Also, if you are using the master branch, try rewinding it to commit 9eb75a5d207aa24a79e8c161ff826c09c828bd18 and see if that fixes it.

blag avatar Feb 24 '17 22:02 blag

hey , i used pip to install this , i don't know which version or commit/branch it ts but will try out last ones . -> about AJAX calls , i see no call from ajax . totally clear . but i figured out there's problem with jquery


$(document).ready(function() {
                chainedfk.init(chainfield, url, id, value, empty_label, auto_choose);
            });
        })(jQuery || django.jQuery);

and also i tried these things in settings but same results :

USE_DJANGO_JQUERY = False
JQUERY_URL = 'http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js'

is there anything wrong with this part ? i copied code from lib , couldn't find any docs

ct = ChainedModelChoiceField(
        queryset=City.objects.all(),
        empty_label=_('select city'),
        label=_('city'),

        to_app_name='core', to_model_name='city', chained_field='province', chained_model_field='province',
        foreign_key_app_name='core', foreign_key_model_name='address', foreign_key_field_name='city',
        show_all=False, auto_choose=False,

    )

mohandeath avatar Feb 25 '17 08:02 mohandeath

I just released version 1.3.3 to PyPI and GitHub, although I doubt that contains fixes for this issue.

Take a look at this suggestion in #116 and see if that works for you.

blag avatar Mar 08 '17 12:03 blag

@mohandeath - Thanks for the example. I had a hard time figuring out how to use ChainedModelChoiceField in a form! Your code helped me figure the last bits out.

@blag, would you be open to accepting a pull request that adds documentation on how to use ChainedModelChoiceForm in a form?

alokshenoy avatar Mar 24 '17 09:03 alokshenoy

@alokshenoy Absolutely! 😄

blag avatar Mar 24 '17 20:03 blag

tried latest version and still not fixed ,

mohandeath avatar Apr 09 '17 11:04 mohandeath

Is there any update on this issue. or is there a correct documentation. Can you please reply @blag

rakeshgm avatar Jul 20 '17 18:07 rakeshgm

@rakeshgm Sorry I have been extremely busy and haven't touched my open source projects in awhile. I don't have any update on this issue. At this point I'm simply awaiting a pull request from @alokshenoy fixing the documentation.

If you would like to assist me in debugging this, you can clone the master branch and dig through the code to figure out why it isn't working.

Sorry I don't have a more useful update. 😕

blag avatar Jul 20 '17 22:07 blag

Gimme about 2 weeks to get this done. Gotten busy with work.

alokshenoy avatar Jul 20 '17 23:07 alokshenoy

after adding

<script type="text/javascript" src="{% static 'smart-selects/admin/js/chainedfk.js' %}"></script>
<script type="text/javascript" src="{% static 'smart-selects/admin/js/chainedm2m.js' %}"></script>
<script type="text/javascript" src="{% static 'smart-selects/admin/js/bindfields.js' %}"></script>

the form works. see dungu's answer

3spppy avatar Jan 08 '18 10:01 3spppy

Hey @alokshenoy - have you found any time to work on this?

blag avatar Jan 09 '18 06:01 blag

Any update? I've tried all the solutions suggested here but nothing has worked yet. I do not see any ajax at all

ThomasAriano avatar Mar 04 '18 06:03 ThomasAriano

@ThomasAriano Fire up the developer tools in your web browser and check if the AJAX request is getting sent out, find what (if anything) is being returned in the response, and post it here. If the AJAX request isn't being fired, see if there are any errors or warnings in the javascript console and post them here.

blag avatar Mar 12 '18 04:03 blag

@blag Any update? I've tried all the solutions suggested here but nothing has worked yet. I do not see any ajax at all. please replay soon

vishwa8545 avatar Jan 04 '19 01:01 vishwa8545

Is there any documentation regarding ChainedModelChoiceField struggling to find a solution

albixhafa avatar Jan 28 '20 23:01 albixhafa

Please anyone having this issue test again with latest release and report, thanks.

manelclos avatar Aug 18 '20 05:08 manelclos

Yo al dia de hoy sigo con el mismo problema :c

diego1996 avatar Oct 30 '22 00:10 diego1996