user3441273
user3441273

Reputation: 45

match string with database values in django

I have a database table 'travel'.There is a column called travelclasss and it contains values 'economy,business,firstclass'.I have a form

<form id="search-form">
        FROM<input type="text" name="f"><br>
        TO<input type="text" name="t"><br>
        CLASS<input type="text" name="c"><br>
        <input type="submit" value="Search">
    </form>

If I enter in my form CLASS field either economy 'or' business 'or' firstclass I want to check these names are in my database travelclasss column.

I need implement it in django. How to check particular name is present in database using django.

Upvotes: 0

Views: 157

Answers (1)

Mihai Zamfir
Mihai Zamfir

Reputation: 2166

You can do like this:

models.py

from django.db import models

class Travel(models.Model):
    TRAVEL_CHOICES = (
        ('EC', 'economy'),
        ('BS', 'business'),
        ('FC', 'firstclass'),
    )
    travelclasss = models.CharField(max_length=2,
                                    choices=TRAVEL_CHOICES,
                                    default='EC')

forms.py

from django.forms import ModelForm
from yourapp.models import Travel

class TravelForm(ModelForm):
     class Meta:
         model = Travel
         fields = ['travelclasss']

Then, you will have a drop-down box to choose from.

Here is more doc on ModelForms. Here is more doc on Choices.

Upvotes: 5

Related Questions