B
B
blackbb2015-11-30 22:59:34
Django
blackbb, 2015-11-30 22:59:34

How to make a specific field by user in django?

Hello. There is a model:

class Numbers(models.Model):
    title = models.CharField(max_length=200, verbose_name="Название",unique=True)
    hotel = models.ForeignKey(Hotels, blank = True, verbose_name='Отель или гостевой дом, если есть')
    slug = models.SlugField(verbose_name="URL")
    sleep_place = models.IntegerField(verbose_name='Спальных мест')
    room = models.IntegerField(verbose_name='Количество комнат')
    image = models.ImageField(default = 'Noimage', verbose_name="Главное фото")
    user = models.ForeignKey(User)

and form:
class NumbersForm(forms.ModelForm):
    class Meta:
        model = Numbers
        fields = '__all__'
        exclude = ['user']

and views
class NumberCreate(CreateView):
    model = Numbers
    template_name = 'add_numbers.html'
    form_class = NumbersForm
    success_url = '/user/success'

When adding a room, the form has a hotel field where the user selects a hotel, but he must select the hotel that he added, and now when choosing a hotel, all hotels are displayed. The Hotels model also has a user field. The user first adds the hotel then the number. So how to implement so that the choice is only from the hotels added by the user?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
blackbb, 2015-12-03
@blackbb

Разобрался

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question