0

I am using Django 1.11 and mysql for a web database. I have table for cars ads post. This table has many columns such as title, price, time of the posted ads, and so on, there are many columns for car features like type, condition, cylinders, fuel. Each column of them has many choices. for example, for status there are choices of (excellent, very good, good, poor). For fuel gas, diesel. For cylinder choices are 3,4,5,6,8,12. And so on. I have two options to implement this.

1- The first option is to make table for cars contain columns that does not have options like title. Then connect this table to other tables (table for type, model, fuel, cylinders, and so on). Then connect each table to the main cars table in many to one relationship.
2- The second option is to make tuples inside Django model have these choices and make fields inside the main table instead of making every column individual table and then connect them through foreign key.

My question is that, which option is more effective from the prospective of:

1- Performance and speed

2- Easy to make forms, and to write and save to database the data comes up from the forms.

Saleh
  • 284
  • 1
  • 3
  • 15
  • This is a primarily opinion based question hence not suitable for Stack Overflow format; but I would strongly suggest against hard coding domain specific data. – Selcuk May 01 '18 at 03:22

1 Answers1

1

1- The first option is to make table for cars contain columns that does not have options like title. Then connect this table to other tables (table for type, model, fuel, cylinders, and so on). Then connect each table to the main cars table in many to one relationship.

I'm presuming that you want your models to be like this:

class CarCondition(models.Model):
    condition_id = .. # the primary key, preferably an AutoField
    condition_name = models.CharField(unique=True)

And then you would populate CarCondition like this:

condition_id | condition_name
1            | 'Excellent'
2            | 'Very good'

etc. And then in Car:

class Car(models.Model):
    title = models.CharField()
    ...
    condition = models.ForeignKey('CarCondition', on_delete=...)
    ...

The ModelForm:

class CarAdForm(forms.ModelForm):
    class Meta:
        model = Car

And that's it, because all the foreign keys are converted to ModelChoiceFields by Django.

You can save the data like this:

CarAdForm(req.POST).save()

in your view.

2- The second option is to make tuples inside Django model have these choices and make fields inside the main table instead of making every column individual table and then connect them through foreign key.

Since the columns like car_condition will have a tuple of choices, and will be a CharField, you will have to change a few things about the ModelForm, so that it can be rendered as a select widget. Refer as an example this answer. You may or may not require more than CarAdForm(req.POST).save() depending on what you change about the ModelForm.

It seems easier to create and save forms by the first approach. However, in my opinion, ForeignKey is more about referential integrity, whereas you are just trying to limit user input to a few choices, which is better done by the choices tuple.

codeandfire
  • 445
  • 2
  • 9
  • Thank you so much. I tried to choose your answer as selected answer but unfortunately. My reputation score is less than 15 since I am new here. – Saleh May 01 '18 at 08:41
  • @insaanaakhar if I'm not wrong, voting up requires 15 rep. Accepting an answer doesn't require rep, and you can just click on the tick icon. – codeandfire May 02 '18 at 10:45