A
A
Andrey Salnikov2017-05-18 16:23:35
Django
Andrey Salnikov, 2017-05-18 16:23:35

How to set several different models in ForeignKey?

There are several models.

class Page(models.Model):
    title = models.CharField('Заголовок', max_length=200)
    slug = models.SlugField(null=True, blank=True)
    content = models.TextField()
    template = models.CharField(max_length=200, verbose_name='Название шаблона старницы', blank=True, null=True)

    keyword = models.CharField(max_length=200, verbose_name='Кличевые слова', null=True, blank=True)
    description = models.CharField(max_length=200, verbose_name='Описание', null=True, blank=True)

    created = models.DateTimeField(auto_now_add=True, auto_now=False)

    def save(self, *args, **kwargs):
        self.slug = slugify(unidecode(self.title))
        super(Page, self).save(*args, **kwargs)

    def get_absolute_url(self):
        return reverse('page', kwargs={
            'slug': self.slug,
        })

    def __str__(self):
        return self.title

    class Meta:
        verbose_name = 'Страница'
        verbose_name_plural = 'Страницы'

class News(models.Model):
    title = models.CharField(max_length=200, verbose_name='Заголовок')
    slug = models.SlugField(max_length=200, unique=True)
    pub_date = models.DateField(verbose_name='Дата публикации')
    intro_text = models.TextField(verbose_name='Краткий текст')
    full_text = models.TextField(verbose_name='Полный текст')
    hidden = models.BooleanField(default=False, verbose_name='Скрыт')
    image = models.ImageField(verbose_name='Изображение', null=True, upload_to='news/', blank=True)
    file = models.FileField(verbose_name='Файл',
                            help_text='Прикреплённое изображение', upload_to='file/', null=True, blank=True)
    keywords = models.CharField(max_length=200, verbose_name='Кличевые слова', null=True)
    descriptions = models.CharField(max_length=300, verbose_name='Описание', null=True)

    def save(self, *args, **kwargs):
        self.slug = slugify(unidecode(self.title))
        super(News, self).save(*args, **kwargs)

    def get_absolute_url(self):
        return reverse('news_page', kwargs={
            'slug': self.slug,
        })

    def __str__(self):
        return self.title

    class Meta:
        verbose_name = 'Новость'
        verbose_name_plural = 'Новости'

We need to make another class.
class Slider(models.Model):
    name = models.CharField(max_length=200, verbose_name='Название')
    link = models.ForeignKey()
    def __str__(self):
        return self.name

How to make it so that this model could take from both News and Page?
You need to link to one or the other. How can you do better?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman Kitaev, 2017-05-18
@Shshzik

generic foreign key

S
sim3x, 2017-05-18
@sim3x

If there are several sliders (sic)

class Slider(models.Model):
    name = models.CharField(max_length=200, verbose_name='Название')

class News(models.Model):
    in_slider = model.ForeignKey(Slider)

class Page(models.Model):
    in_slider = model.ForeignKey(Slider)

If slider 1
class News(models.Model):
    in_slider = model.BooleanField()

class Page(models.Model):
    in_slider = model.BooleanField()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question