0

i'm working in OpenEdx and i'm trying to make a registration page that contains custom fields, state and city.

here is my models.py:

class City(models.Model):
    name = models.CharField('city', max_length=100, blank=True)
    uf = models.CharField('UF', max_length=2, choices=STATE_CHOICES)

    def __str__(self):
        return self.name

    class Meta:
        ordering = ('name',)
        verbose_name = 'cidade'
        verbose_name_plural = 'cidades'


class UserProfileExtras(models.Model):
    
    user = models.OneToOneField(
        USER_MODEL,
        null=True,
        on_delete=models.CASCADE
    )

    state = models.CharField(
        verbose_name="State",
        choices=STATE_CHOICES,
        max_length=100,
    )

    city = models.CharField(
        verbose_name="Cidade",
        max_length=100,
        null=True,
    )

and forms.py:

class UserProfileExtrasForm(ModelForm):
 
    class Meta(object):
        model = UserProfileExtras
        fields = ('state', 'city')

    def __init__(self, *args, **kwargs):
        super(UserProfileExtrasForm, self).__init__(*args, **kwargs)
        self.fields['state'].label = _(u"Estado")
        self.fields['state'].error_messages = {
            "required": _(u"Selecione seu Estado."),
            "invalid": _(u"Estado inválido."),
        }

        self.fields['city'].label = _(u"Cidade")
        self.fields['city'].error_messages = {
            "required": _(u"Selecione sua cidade."),
            "invalid": _(u"Cidade inválida"),
        }

i want to make the city dropdown appears only the selected state's cities

thanks

  • check this out, https://stackoverflow.com/questions/25706639/django-dependent-select You have to do it in the client-side or server-side. It's your choice. If client side you can use JS map function or similar method. or you can use the "onselect/onchange" to send Ajax request to pull data from server based on first select. – Isanka Wijerathne Nov 05 '20 at 09:27
  • And this too, https://simpleisbetterthancomplex.com/tutorial/2018/01/29/how-to-implement-dependent-or-chained-dropdown-list-with-django.html – Isanka Wijerathne Nov 05 '20 at 09:28
  • I saw this second link yesterday, but I don't know where the open edx html is to do this – Rodrigo Suigh Nov 05 '20 at 15:22
  • If you are looking for HTML files. then check in template files. including theme. folder. – Isanka Wijerathne Nov 06 '20 at 12:45

1 Answers1

0

That link might Help you out from the offical Documentation fo the Django https://docs.djangoproject.com/en/1.8/ref/models/fields/#choices

from django.db import models

class Student(models.Model):
FRESHMAN = 'FR'
SOPHOMORE = 'SO'
JUNIOR = 'JR'
SENIOR = 'SR'
YEAR_IN_SCHOOL_CHOICES = (
    (FRESHMAN, 'Freshman'),
    (SOPHOMORE, 'Sophomore'),
    (JUNIOR, 'Junior'),
    (SENIOR, 'Senior'),
)
year_in_school = models.CharField(max_length=2,
                                  choices=YEAR_IN_SCHOOL_CHOICES,
                                  default=FRESHMAN)