jonnyd42
jonnyd42

Reputation: 500

Django select multiple objects and save user choices

I need help getting user preferences to save to the db. I have a set of community objects defined by the following model:

class Community(models.Model):
    name = models.CharField(max_length=20, unique=True)
    banner = models.ImageField(upload_to="communityBanners", blank=True)
    creation_date = models.DateTimeField(default=timezone.now)

Each user has a profile that looks like the following:

class UserProfile(models.Model):
    user = AutoOneToOneField(settings.AUTH_USER_MODEL, primary_key=True,
                             on_delete=models.CASCADE)
    points = models.DecimalField(default=0.0, max_digits=10, decimal_places=2)
    credScore = models.DecimalField( default=0.0, max_digits=4, decimal_places=2 )
    website = models.URLField(blank=True)
    avatar = models.ImageField(upload_to="profilePics", blank=True)

    community_preferences = models.ManyToManyField(Community)

I've made it so that users are able to create new communities. However, I want to also make it so that other users can go to a multiple choice form and select which communities they want to join, then hit submit and store the preferences. Since the communities are objects, how do I create a multiple choice object field and form that they can select those and then save their preferences?

Could someone help show me how to structure the following: form, html template to display the options, and how to properly save the preferences?

I'm under the impression that ModelChoiceField will not work here since I want to be able to select one, all, or any number in between of objects.

EDIT:

Here is the new form:

class CommunitySelectForm(forms.Form):
    communities = forms.ModelMultipleChoiceField(queryset=Community.objects.all(), widget=forms.CheckboxSelectMultiple)

Here is my view:

def joinCommunities(request, user_id):
    #user_ob = get_user_model().objects.filter(id=user_id).first()
    #full_user_profile = UserProfile.objects.filter(user=user_ob).first()

    if request.method == 'POST':
        form = CommunitySelectForm(request.POST)
        if form.is_valid():
            comms = form.save(commit=False)
            comms.user = request.user
            comms.save()
    else:
        form = CommunitySelectForm()
    print(form)
    return render(request, 'qa/select_communities.html', {'form' : form})

And here is the template that doesn't seem to be loading anything:

{% extends 'qa/base.html' %}
{% load staticfiles %}
{% load gravatar %}
{% block content %}

<div>
    <div class="col-sm-8 input">
      <form method="post" enctype='multipart/form-data'>
          {% csrf_token %}    
            {% for field in form %}
                <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-10">


                        <span class="text-danger small">{{ field.errors }}</span>
                    </div>
                    <label class = "control-label col-sm-2">{{ field.label_tag }}</label>
                    <div class="col-sm-10">{{ field }}</div>
                </div>
            {% endfor %}
          <input class="btn btn-submit pull-right" href="/profile/{{ user.id }}" type="submit" value="Choose Communities" />
      </form>
    </div>
</div>

{% endblock content %}

I've also set up the URL for joining the communities as follows:

 url(r'^join-communities/(?P<user_id>\d+)/$', views.joinCommunities, name='qa_join_communities')

The issue is that when I submit the form it doesn't save and I get this error:

'CommunitySelectForm' object has no attribute 'save'

Upvotes: 0

Views: 4174

Answers (1)

Ojas Kale
Ojas Kale

Reputation: 2159

You can create form as follows:

from django import forms


class CommunitiesSelectForm(forms.Form):
    communities = forms.ModelMultipleChoiceField(queryset=Community.objects.all(),widget=forms.CheckboxSelectMultiple)

This would render a form with checkbox for communities. Then in your views inside post method

get current user and get communities from submitted form and link them.

{% extends 'qa/base.html' %}
{% load staticfiles %}
{% load gravatar %}
{% block content %}

<div>
    <div class="col-sm-8 input">
      <form method="post" enctype='multipart/form-data'>
          {% csrf_token %}    
          {{ form }}  
          <input class="btn btn-submit pull-right" href="/profile/{{ user.id }}" type="submit" value="Choose Communities" />
      </form>
    </div>
</div>

Set your template like this.
{% endblock content %}

Upvotes: 1

Related Questions