Django : submitting Foriegn key field with or without form object to models

we can submit foriegnkey data to models through ModelForm

class BooksForm(forms.ModelForm):
    class Meta:
        model = Books
        fields = "__all__"

where in templates we can add

{{}} (author is foriegnkey field in  books model)

Im aware that we can submit foriegnkey data using forms like this

but my question there any way where we can submit a foriegnkey object which we have fetched using some other method (with or without form ) to a model(in my case its book)

Let me explain it in detail

lets say for instance there is a Search bar when users search for author, then the function fetches list of authors (choice field) to the user where user can select and submit

which should get populated in the books model

there isnt any proper info related to this on web all i could see is information on how to save data with Foriegnnkey using model form

any kind of insights is appreciated

I'm not 100% sure what your desired outcome is - this is how I understand your issue:

If you want to create a Book entry while passing an Author instance along you could set it as follows:


class Author(models.Model):
   name = models.CharField(max_length=50)

class Book(models.Model):
   author = models.ForeignKey('Author', on_delete=models.CASCADE)
   title = models.CharField(max_length=50)

def example_view(request):
   selected_author_instance = Author.objects.get(pk=1)  # example query, adjust to your needs
   # Create Book instance and pass author instance along
   book_instance = Book.objects.create(

   return render(...)


Константы Python: Улучшение управляемости вашего кода

Современный Python: начинаем проект с pyenv и poetry

Настройка проекта Python — виртуальные среды и управление пакетами

Использование requests в Python — тайм-ауты, повторы, хуки

Понимание декораторов в Python

ProcessPoolExecutor в Python: полное руководство

map() против submit() с ProcessPoolExecutor в Python

Понимание атрибутов, словарей и слотов в Python

Полное руководство по slice в Python

Выпуск Django 4.0

Безопасное развертывание приложения Django с помощью Gunicorn, Nginx и HTTPS

Автоматический повтор невыполненных задач Celery

Django REST Framework и Elasticsearch

Докеризация Django с помощью Postgres, Gunicorn и Nginx

Асинхронные задачи с Django и Celery

Релизы безопасности Django: 3.2.4, 3.1.12 и 2.2.24

Выпуски исправлений ошибок Django: 3.2.3, 3.1.11 и 2.2.23

Эффективное использование сериализаторов Django REST Framework

Выпуски безопасности Django: 3.2.2, 3.1.10 и 2.2.22

Выпущенные релизы безопасности Django: 3.2.1, 3.1.9 и 2.2.21

View all tutorials →