[Answer]-Adding properties to django model

1👍

I would do the following:

  • Create a separate class for categories and country.
  • Use foreign keys to establish the relationships between categories, countries, and articles.
  • You can create a recursive relationship so that each category can have a parent
    (i.e. parent = models.ForeignKey("self", blank=True, null=True, related_name='children'))

0👍

country is an attribute with a static set of choices. As such, I’d use a static list of countries (in fact, I have one here for you, extracted form pycountry 0.14.8). As the choices are freely available in your Django application, this saves database hits. As long as an article is attributed to a single country, I would include it as a field in the Article model (and probably index it as well). Even if you sometimes need multiple countries, a slight alteration of a CommaSeparatedIntegerField could provide for an easy solution.

Categories are more dynamic in nature, and often more categories are added or some of them are changed or removed. For this a separate model is a good idea. As RohitJ suggested, you can have a model with a recursive relation to itself to allow parent categories. Many existing apps also provide category models that easily be incorporated in your project, e.g. django-categories which I use in my current project.

Leave a comment