0

I have several fields in my models that share the same caracteristics and I would like to declare them in one line with something like this:

class Shop(models.Model):
    id, name, brand = models.CharField(max_length=12)

Or:

class Shop(models.Model):
    id = name = brand = models.CharField(max_length=12)

instead of doing this:

class Shop(models.Model):
    id = models.CharField(max_length=12)
    name = models.CharField(max_length=12)
    brand = models.CharField(max_length=12)

But Django complains with a TypeError in the first case saying that 'CharField' object is not iterable, and with admin.E108 error in the second case.

So my question is how can I declare multiple fields that share the same caracteristics in a single line of code with Django? it looks to me that the second cases is not related to Python.

1 Answer 1

2

You can do something like this...

id, name, brand = [models.CharField(max_length=12) for i in range(3)]
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! it will make my models.py thinner :)

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.