Answer the question
In order to leave comments, you need to log in
How to automatically populate a ForeignKey field in Django?
Hello. There are two models :
class Hotels(models.Model):
vip = models.BooleanField(default = False, verbose_name = 'VIP размещение')
title = models.CharField(max_length=200, verbose_name="Название")
slug = models.SlugField(unique=True, verbose_name="URL")
category = models.ForeignKey(Category, verbose_name='Раздел обьявления')
place = models.ForeignKey(Place, verbose_name='Место расположения')
class Numbers(models.Model):
title = models.ForeignKey(NumberTitle, verbose_name='Название')
hotel = models.ForeignKey(Hotels, blank = True, null=True, verbose_name='Отель или гостевой дом, если есть')
place = models.ForeignKey(Place, default="", verbose_name='Место расположения')
Answer the question
In order to leave comments, you need to log in
Why implement it in a form? It's better to do it in pre_save Numbers.
@receiver(pre_save, sender=Numbers)
def fill_place(sender, instance, **kwargs):
instance.place = instance.hotel.place
After creating the hotel, we send the user to the hotel page and by the URL it will be clear to you which hotel the room is being added to
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question