jazzband / django-smart-selects

chained and grouped selects for django forms
https://django-smart-selects.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
1.12k stars 352 forks source link

Trouble using Smart-Select forms.py #184

Open mohandeath opened 7 years ago

mohandeath commented 7 years ago

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 . :)

blag commented 7 years ago

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.

mohandeath commented 7 years ago

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,

    )
blag commented 7 years ago

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.

alokshenoy commented 7 years ago

@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?

blag commented 7 years ago

@alokshenoy Absolutely! 😄

mohandeath commented 7 years ago

tried latest version and still not fixed ,

rakeshgm commented 7 years ago

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

blag commented 7 years ago

@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. 😕

alokshenoy commented 7 years ago

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

3spppy commented 6 years ago

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

blag commented 6 years ago

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

ThomasAriano commented 6 years ago

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

blag commented 6 years ago

@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.

vishwa8545 commented 5 years ago

@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

albixhafa commented 4 years ago

Is there any documentation regarding ChainedModelChoiceField struggling to find a solution

manelclos commented 4 years ago

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

diego1996 commented 1 year ago

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