0

I am making a web-application with Django and I want to store application logic. What is the most optimal way to go about this? For example, I currently have something resembling this stored in my models.py but it seems sub-optimal to say the least.

class Alphabet(models.Model):
    first = models.CharField(max_length=1, default = 'a')
    second = models.CharField(max_length=1, default = 'b')
    third = models.CharField(max_length=1, default = 'c')

class Digits(models.Model):
    first = models.CharField(max_length=1, default = '1')
    second = models.CharField(max_length=1, default = '2')
    third = models.CharField(max_length=1, default = '3')

Characters = [Alphabet, Digits]

Should I even be storing this kind of data in my models.py folder?

2 Answers 2

1

You can use property decorator for that: python property. As you can see in model methods document, you can define a function as property oof a model. Just return the static data you want to use. I recommend you to define them in models file, because writing them to database is inefficient.

Sign up to request clarification or add additional context in comments.

2 Comments

To clarify, are you suggesting I structure the field like so: first = property('a')
Nope, in the second link about model methods, there is a propert named full_name which is a function returns full name with first and last name values. Just define property functions to return static values.
1

If it doesn't change, why not put it into your application? If you need it in the database, having a special table just for static data seems like a reasonable decision. Or you could have a special table entry that has all your default values (defaults) with a special name, for example "default" instead of a normal name.

1 Comment

@Nailuj that data would definitely be in application logic, no reason to put it into the database - it never changes, changing it would make no sense and it would break the whole application. But if you have to have it in the database: just make a table named "categories" with category name, then category attributes (for example, each chess piece would have it's type of movement, if it can move backwards and how fast can it move).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.