How to make a subcategory belong to a specific category in Django

129
February 16, 2018, at 11:27 PM

I have a little problem with display subcategories belongs only in specific categories. For example: I have a categories: Business, Factory, Health ... and after I can add subcategories for example Own Company in category Business. In this case if I choose category Business I need to have only visible subcategories belongs to this category. How to make it? This is my code. models.py

from django.db import models
from slugify import slugify
class Kategoria(models.Model):
    name = models.CharField(max_length=50, unique=True, verbose_name='Nazwa kategorii')
    slug = models.SlugField(verbose_name='Adres SEO')
    class Meta: 
        verbose_name_plural = 'Kategorie'
    def save(self, *args, **kwargs):
        self.slug = slugify(self.name)
        super(Kategoria, self).save(*args, **kwargs)
    def __str__(self):
        return self.name
class Subkategoria(models.Model):
    category = models.ForeignKey('Kategoria', related_name='subkategoria', on_delete=models.CASCADE, blank=True, null=True, verbose_name='Kategoria główna')
    name = models.CharField(max_length=50)
    class Meta:
        verbose_name_plural = 'Subkategorie'
    def __str__(self):
        return self.name
class Strona(models.Model):
    name = models.CharField(max_length=250, verbose_name='Nazwa strony')
    slug = models.SlugField(verbose_name='Adres SEO')
    www = models.CharField(max_length=200, verbose_name='Adres strony', unique=True)
    content = models.TextField(verbose_name='Opis')
    category = models.ForeignKey('Kategoria', verbose_name='Kategoria', on_delete=models.CASCADE)
    subcategory = models.ForeignKey('Subkategoria', verbose_name='Subkategoria', on_delete=models.CASCADE)
    publish = models.DateField(auto_now=False, auto_now_add=False)
    class Meta:
        verbose_name_plural = 'Strony'
    def __str__(self):
        return self.name
READ ALSO
Sum values from two lists using a function - no zip, map

Sum values from two lists using a function - no zip, map

I need solution to sum (addition) of each items of having same indexI find the solution using map, zip, list comprehension

138
event owner identification using watchdog in python

event owner identification using watchdog in python

We have an admin account (eg abc) which is used to login to a linux server and delete some particular set of filewe have created a watchdog filesystem eventhandler and scheduled a event tracking on that server (particular folder '/abc/testfolder' to capture...

192
Python 3.6.3: Multiple “Exception ignored in: <generator object…” in flask app

Python 3.6.3: Multiple “Exception ignored in: <generator object…” in flask app

I'm running a flask app, upgraded everything from Python 27 to 3 about 5 months ago

150
How to delete record from dbf file using python dbf module?

How to delete record from dbf file using python dbf module?

I'm trying to write/delete records in a visual foxpro 6 dbf file, using python 27 and the dbf package:

307