1

I am Learning Django and i need to allow users of the app to be able to add more options to the item_name field through the template but i don't have an idea on how to achieve that. Thanks for the help.

Here is my model

class ItStore(models.Model):
    type_choice = (
            ('Printer Catridge', 'Printer Catridge'),
            ('UPS', 'UPS'),
            ('UPS Battery', 'UPS Battery'),
            ('Mouse', 'Mouse'),
            ('Keyboard', 'Keyboard'),
        )
    item_name = models.CharField(max_length='100', blank=True, null=False, choices=type_choice)
    quantity = models.IntegerField(default='', blank=True, null=False)

Here is my View

def itstore_create(request):
    form = ItStoreCreateForm(request.POST or None)
    submit = "Create IT Store Items"
    if form.is_valid():
        instance = form.save(commit=False)
        instance.save()
        message = instance.item_name + " Successfully Created"
        messages.success(request, message)
        return redirect("items:itstore_list")
    context = {
        "form": form,
        "title": "CREATE ITEM", 
    }
    return render(request, "store_form.html", context)

Here is my form

class ItStoreCreateForm(forms.ModelForm):
    class Meta:
        model = ItStore
        fields = ['item_name', 'quantity']
simplyvic
  • 213
  • 6
  • 17
  • What are the additional options based on? In other words, when do you add additional options? – Shang Wang Jun 16 '16 at 19:47
  • additional fields can be added if a user wants to select an item which is not in the current choice list – simplyvic Jun 16 '16 at 20:14
  • Then you shouldn't be using the same dropdown. The `choices` on `item_name` is set when the form initializes, so any new choices that not in `type_choice` would give you `Select a valid choice. That choice is not one of the available choices` error. – Shang Wang Jun 16 '16 at 20:28
  • You could modify the `choices` when the form initializes though, example: http://stackoverflow.com/questions/3419997/creating-a-dynamic-choice-field – Shang Wang Jun 16 '16 at 20:29

1 Answers1

1

You could not define choices= on your model. But instead define a list of default choices outside of the model.

my_choices = (
    "foo",
    "bar",
    "pop",
)
class MyModel(models.Model):
    my_field = models.CharField(max_length=100)

Then in your view you'd want to import that tuple and pass it to you template:

from my_app.models import my_choices

def my_view(request, *a, **kw):
    # view logic
    return render(request, "path/to/my/template", choices=my_choices)

Then in your template you can have a select box with the default choices and string values. And also have an optional input type=text that will save to that field if populated.

Something like:

<select name="my_field">
<option value="" selected="selected">-----</option>
{% for choice in choices %}
    <option value="{{ choice }}">{{ choice }}</option>
{% endfor %}
</select>

Will give you default choices. Then add an input with the same name, this will act as an optional new choice.

<input type="text" name="my_field"/>

Optionally you could write javascript logic that will ensure only the selectbox or the textfield gets submitted.

marcusshep
  • 1,916
  • 2
  • 18
  • 31
  • "You could not define choices= on your model." This was basically my thought. If `choices` is moved into its own class in the DB, then adding/removing becomes straightforward. You may want to exert some level of moderation, but that's a different subject entirely. – Peter Rowell Jun 17 '16 at 02:21
  • marcusshep, Am new to django and i dont realy understand what you are talking about. can you please use code to give examples. thanks – simplyvic Jun 17 '16 at 08:38