Database and class design Django

vib

I am building a website for selling bikes in django. I have a class Bike that will contain the bikes; it looks like this :

class Bike(models.Model):
    brand = models.CharField(max_length=100)
    model = models.CharField(max_length=100)

Now I would like to add a field wheels describing the wheels of the bike, and I would like this field to contain possibly several fields like the brand, size of the wheel. I would like these details on wheels implementation to be separated from the bike's class specification; however, I expect each wheel to be associated to exactly one bike.

An idea I had was to do the following :

class Wheels(models.Model):
    description = models.CharField(max_length=100)
    size = models.DecimalField(max_digits=5,decimal_places=2)

and then to include a new field in my bike :

class Bike(models.Model):
    # previous fields
    wheels = models.ForeignKey(Wheels)

I have however some doubts about it :

1) is it the correct design ? If I do that I will end up with a wheels' database which I don't think I actually need. I just want to have flexible fields in my bike database. Basically I expect that I will have a one to one relationship between bikes and wheels.

2) If this is the correct design, then what I would like is to be able to add wheels on the fly while adding bike (never have to add wheels separately). What is the best way to do that ?

Thanks a lot for any hint / reference. I am a beginner with django...

Chris Berragan
  1. That design looks good to me - it's a good idea to keep individual objects in separate tables (in this case the components that make up the bike - particularly since they can be sold separately, and bikes can be customised with different parts)

  2. I think a simple preset class inherited from the Bicycle class should do the trick:

    class BicycleFrame(models.Model):
        brand = models.ForeignKey(Brand)
        model = models.CharField(max_length=100)
    
    # class BicycleWheels, BicyclePedals etc..
    
    class Bicycle(models.Model):
        frame = models.ForeignKey(BicycleFrame)
        wheels = models.ForeignKey(BicycleWheels)
        pedals = models.ForeignKey(BicyclePedals)
        # etc ...
    
    class PresetBicycle(Bicycle):
        pass
    
    class PurchaseableMixin(models.Model):
        user_id = models.ForeignKey(Customer)
        def purchase(self):
            # ... call this in the form handler to save this
            # instance of a bike in the database and maybe
            # email the customer to inform them of their purchase etc..
        class Meta:
            abstract = True
    
    class PurchasedBicycle(Bicycle, PurchaseableMixin):
        pass
    

.. then you can create a PresetBicycle in your admin area then in the view that you show to customers you could display the PresetBicycle by default and also provide a form to purchase it that is auto filled with the details for the PresetBicycle and that creates an instance of a PurchasedBicycle on submission (a JS framework like ReactJS and Backbone, or Angular might be best for the customer view).

Hope this helps!

P.S. Note that I haven't tested this approach myself - I'd suggest creating a few new branches in your Version Control (eg git) and setting up separate settings files for each one (import from a base settings file and use a separate database for each one to save having to mess around with too many migrations and obsolete tables) to test several approaches before making your final decision - it's good to figure this stuff out early on so that you don't end up making big structural changes later on.

P.P.S Also not that I've changed the brand field to be a ForeignKey, since you may later wish to filter on the brand..

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Django database design

From Dev

Design pattern for database manager class

From Dev

design database for class with groups and students

From Dev

design database for class with groups and students

From Dev

Mapping Class Diagram into database table design

From Dev

Relational Database Design - Create an Entity Class for Payment Type?

From Dev

Design pattern for database access library and abstract class as a return type

From Dev

Add a property to class but not store in database, django?

From Dev

Django - Database design for many to many relationship involving an attribute of the relationship

From Dev

Django Database Design With Intermediate Table- How With This Instance?

From Dev

Django PostgreSQL database table design, foreign keys, 1 to many

From Dev

database design in django models for multiple columns for multiple rows.

From Dev

url and django database no object found matching the query with identical working class

From Dev

Django database migration issue - missing "Migration class" error

From Dev

My database design for movie database

From Dev

How to design a database in the way that each class has one teacher and one TAs?

From Dev

Django turn based browser game - database design to handle separate time steps

From Dev

Related to database schemas and design

From Dev

Database design decision

From Dev

Database Design and normalization in chess

From Dev

Access Database theory/design

From Dev

A database design technique

From Dev

Followers - mongodb database design

From Dev

Database design of a WYSIWYG field

From Dev

Insertion anomaly in database design

From Dev

Database design for hundred of tables

From Dev

Better Database design for hibernatre?

From Dev

Database design for dentist app

From Dev

Database design for optimisation

Related Related

  1. 1

    Django database design

  2. 2

    Design pattern for database manager class

  3. 3

    design database for class with groups and students

  4. 4

    design database for class with groups and students

  5. 5

    Mapping Class Diagram into database table design

  6. 6

    Relational Database Design - Create an Entity Class for Payment Type?

  7. 7

    Design pattern for database access library and abstract class as a return type

  8. 8

    Add a property to class but not store in database, django?

  9. 9

    Django - Database design for many to many relationship involving an attribute of the relationship

  10. 10

    Django Database Design With Intermediate Table- How With This Instance?

  11. 11

    Django PostgreSQL database table design, foreign keys, 1 to many

  12. 12

    database design in django models for multiple columns for multiple rows.

  13. 13

    url and django database no object found matching the query with identical working class

  14. 14

    Django database migration issue - missing "Migration class" error

  15. 15

    My database design for movie database

  16. 16

    How to design a database in the way that each class has one teacher and one TAs?

  17. 17

    Django turn based browser game - database design to handle separate time steps

  18. 18

    Related to database schemas and design

  19. 19

    Database design decision

  20. 20

    Database Design and normalization in chess

  21. 21

    Access Database theory/design

  22. 22

    A database design technique

  23. 23

    Followers - mongodb database design

  24. 24

    Database design of a WYSIWYG field

  25. 25

    Insertion anomaly in database design

  26. 26

    Database design for hundred of tables

  27. 27

    Better Database design for hibernatre?

  28. 28

    Database design for dentist app

  29. 29

    Database design for optimisation

HotTag

Archive