Monday, October 30, 2017

Re: MultipleChoiceField records down choices as a list, but CharField converts them to a list?



On Thu, Oct 26, 2017 at 7:54 AM, Jack <valachio123@gmail.com> wrote:
I have a model field for choosing multiple options.  Here is the code for models and forms:

models.py:
    CONDO_APARTMENT = 'Condo Apartment'
    DETACHED_HOUSE = 'Detached House'
    SEMI_DETACHED = 'Semi-detached'
    TOWNHOUSE = 'Townhouse'
    
    PROPERTY_TYPE = (
        (CONDO_APARTMENT, 'Condo Apartment'),
        (DETACHED_HOUSE, 'Detached House'),
        (SEMI_DETACHED, 'Semi-detached'),
        (TOWNHOUSE, 'Townhouse'),
    )

    property_type = models.CharField(max_length=50, help_text="You can select more than 1 option")


forms.py:
    property_type = forms.MultipleChoiceField(widget=forms.SelectMultiple, choices=BuyerListing.PROPERTY_TYPE)


Let's assume the selected choices were 'Condo Apartment' and 'Semi-detached'.  The value stored on my database is this - ['Condo Apartment', 'Semi-detached']


Given your model definition, that means that a Python list object was converted to a string and stored in a single database field, which means that you no longer have a list, you have a Python string that looks like a list. 

If your model can have multiple values associated to a single object for a single field, consider a M2M relationship with a table containing all of the available options.

 

Now this is in a list format, which makes sense, but it seems to have been converted to a string.  When I try to call on property_type in a .html document...

{% for property in model.property_type %}
    <p>{{ property }}</p>
{% endfor %}


This is slightly confusing. Is 'model' a context variable that you are providing in your view? I'm assuming this refers to the object being pulled from the DB. If that's the case, then this code is roughly equivalent to the following:

for x in "['Condo ap'":
    print(x)


You likely just need {{ model.property_type }} given your model definition.

 

The result is displayed in singular characters, like this:

[
'
C
o
n
d
o
 
a
p


Happens a lot in templates when they go too deep with the {% for %} tags.

 
.. and so on.  Instead I want the result to be the values in the list, like this:

Condo apartment
Semi-detached

How do I do this?  I tried experimenting with different model field types but CharField seems like the only appropriate one for MultipleChoiceField.


I think you're intermixing Model fields and Form fields, both of which have very different functions. I did the same thing when I first started with Django:


These are not interchangeable as they have different purposes. 

It's not necessarily clear what you are trying to do. Your template code indicates one thing, and your textual description another.

According to your template code, you are simply displaying a static unordered list of text blurbs on the page, no form elements involved. From your other descriptions,  you are trying to display a select form control with multiple options in it. A bit more guidance here would result in a better answer.

-James

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2Be%2BciVDYn%2B1x4%2BF3bDu1rc%2B0pbj%2BVyZFtUXGJm9inNdZ6i2rw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment