0

What I'm trying to accomplish here is how to actually do a chained dropdown when there's no authentication. I've managed to create a form using chained dropdowns but those were tied to the 'current user'.

Now my problem is that i need to do exactly the same but on a form where you don't need to authenticate and I'm not sure how to do the relationship between the dropdowns.

models.py

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    business = models.ForeignKey(Business)
    role = models.ForeignKey(Role)
    telephone = models.CharField(max_length=20)
    title = models.CharField(max_length=60)

forms.py

class UserProfileForm(forms.ModelForm):
class Meta:
    model = UserProfile
    fields = ('business', 'role', 'telephone', 'title',)

views.py

def register(request):

if request.method == 'POST':
    profile_form = UserProfileForm(data=request.POST)

    # If the form is valid...
    if profile_form.is_valid():
        profile = profile_form.save(commit=False)
        profile.user = user

        # Now we save the UserProfile model instance.
        profile.save()

        # Redirect after succesful form submssion
        return HttpResponseRedirect('/home/')

    else:
        print (profile_form.errors)

else:
    profile_form = UserProfileForm()

return render(request,
              'home/register.html',
              {'profile_form': profile_form, 'registered': registered})

register.html

<form>
    {% csrf_token %}

    <div class="form-group">
        {{ profile_form | crispy }}
    </div>

    <input type="submit" name="submit" value="Register"/>
</form>

Now when that webpage gets rendered i do get the dropdowns for business and role(ALL of them), but i haven't been able to find how to chain what roles to display based on what business has.

Hopefully I explained myself.

thank you,

1

1 Answer 1

0

You should update the dependent fields based on selection of another. This needs to happen client side. You will populate the first field with options but leave the other ones empty. Once the first option is selected, you can launch an ajax request to the server and retrieve the options filtered by the previous selection. This means you will need to have a url endpoint that will return these to you in a format that best suits you (either a REST like interface or rendered html). Once you get the results, you will populate the required field and then chain to the next one. Remember to bind your newly generated html otherwise you will not be able to catch the actions.

Sign up to request clarification or add additional context in comments.

2 Comments

well not to sound offensive but the reason of my question is because after trying to it using django querysets i came to the same conclusion that i would need jquery or ajax, en well you answer with no example or even link for me to look at don't find it very useful i'm sorry
I am sorry if you feel that way but writing the entire application for you will not work. I gave you a few guidelines so you can move further in your investigation. If you give me a sample of code where you are using this and asking what is wrong with it, it would be my pleasure to help you debug. Otherwise there are plenty of tutorials out there on how to capture javascript events and launch an ajax request (jquery or not) and how to setup a django url mapping and return for instance a json.dums(<my dictionary>) under a httpresponse.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.