Answer the question
In order to leave comments, you need to log in
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 = 'Новости'
class Slider(models.Model):
name = models.CharField(max_length=200, verbose_name='Название')
link = models.ForeignKey()
def __str__(self):
return self.name
Answer the question
In order to leave comments, you need to log in
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)
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 questionAsk a Question
731 491 924 answers to any question