Romain Sconza
Romain Sconza

Reputation: 15

Django form with dropdown list using Database returns empty fields

I'm discovering Django and I'm trying to develop a simple application.

I have three tables in my database : One big table to report all the information to users and 2 tables to create drop down list on my form (but no usage of foreign keys on purpose). I need to have these two tables because statuses and areas need to be editable at all time and need to be written in the same way each time in the main table Action.

Here is my model.py :

class Status(models.Model):
    id_status = models.AutoField(db_column='ID_STATUS', primary_key=True)
    status = models.CharField(db_column='STATUS', max_length=50)

    def __str__(self):
        return self.status

    class Meta:
        managed = False
        db_table = 'T_EAVP_STATUS'


class Area(models.Model):
    id_area = models.AutoField(db_column='ID_AREA', primary_key=True)
    area_name = models.CharField(db_column='AREA_NAME', max_length=50)

    def __str__(self):
        return self.area_name

    class Meta:
        managed = False
        db_table = 'T_EAVP_AREA'


class Action(models.Model):
    id = models.AutoField(db_column='ID', primary_key=True)
    title = models.CharField(db_column='TITLE', max_length=200)
    due_date = models.DateTimeField(db_column='DUE_DATE')
    status = models.CharField(db_column='STATUS', max_length=50)
    date_insert = models.DateTimeField(db_column='DATE_INSERT', auto_now_add=True)
    emitting_area = models.CharField(db_column='EMITTING_AREA', max_length=50)
    receiving_area = models.CharField(db_column='RECEIVING_AREA', max_length=50)
    owner = models.CharField(db_column='OWNER', max_length=200)

    def __str__(self):
        return self.title

    class Meta:
        managed = False
        db_table = 'T_EAVP_ACTION'

Here is my forms.py :

class ActionForm(forms.ModelForm):
    status = forms.ModelChoiceField(queryset=Status.objects.all())
    receiving_area = forms.ModelChoiceField(queryset=Area.objects.all())
    emitting_area = forms.ModelChoiceField(queryset=Area.objects.all())

    class Meta:
        model = Action
        fields = ['title', 'due_date', 'owner']

Here is my views.py :

@csrf_exempt
@xframe_options_exempt
def action_add(request):
    if request.method == 'POST':
        form = ActionForm(request.POST)
        if form.is_valid():
            action = form.save()
            return redirect('action-list')
    else:
        form = ActionForm()
    return render(request, 'polls/action_add.html', {'form': form})

Here is my HTML code :

{% extends 'polls/base.html' %}

{% block title %}{{ action.title }}{% endblock %}


{% block content %}
<h1>Ajouter une action</h1>

<form action="" method="post" class="form" novalidation>
  {% csrf_token %}
  {{ form }}
    <br><br>
    <input type="submit" class="submit" value="Créer">
</form>

{% endblock %}

I'm pretty sure I've imported all the needed libraries from Django.

Problem : When I'm running my code and I try to create a new action using my ActionForm, it is not working properly. In the Action table, the fields that correspond to the fields filled by the drop down lists are completely empty.

Table Status and table Area contain values so the problem is not coming from here.

I've tried a lot of different things but nothing seems to work and fields are always empty in my database after the save of the form.

If someone sees a solution, I'm interested !

Upvotes: 0

Views: 192

Answers (1)

Romain Sconza
Romain Sconza

Reputation: 15

The solution was to add the name of the fields in form.py :

class ActionForm(forms.ModelForm):  # Crée un formulaire se basant sur Action
    status = forms.ModelChoiceField(queryset=Status.objects.all())
    receiving_area = forms.ModelChoiceField(queryset=Area.objects.all())
    emitting_area = forms.ModelChoiceField(queryset=Area.objects.all())

    class Meta:
        model = Action
        fields = ['title', 'due_date', 'owner', 'status', 'receiving_area', 'emitting_area']

Thank you Willem Van Onsem !

Upvotes: 1

Related Questions