Issue with creating new pigeon and his parent records in a Django form

Hi everyone,

I am new to programming and currently learning Django. I am working on a web application where users can register new pigeons into a database. The registration form includes fields for the pigeon’s name and a long dropdown menu with all the existing records for the father and the mother, which becomes quite tedious with many pigeons. Is there a strategy to solve this? Something like search fields to allow the user to search and select the father and the mother? And if parents do not exist, to add functionality to register them from there, similar to the admin form?
If the father or mother doesn’t exist in the database, the user should be able to create a new parent record directly from the main registration form without leaving the page.

I have implemented the following model for the Pigeon Model:

python
from django.db import models

class Pigeon(models.Model):
    GENDER_CHOICES = (
        ('male', 'Male'),
        ('female', 'Female'),
    )

    name = models.CharField(max_length=100)
    gender = models.CharField(max_length=10, choices=GENDER_CHOICES)
    father = models.ForeignKey('self', on_delete=models.SET_NULL, null=True, blank=True, related_name='children_set')
    mother = models.ForeignKey('self', on_delete=models.SET_NULL, null=True, blank=True, related_name='children_set')

    def __str__(self):
        return self.name

I’m facing an issue where I can’t seem to create new parent records from the main registration form. Could someone please help me understand what I’m doing wrong and provide guidance on how to implement this functionality correctly?

I would really appreciate any help or advice you can offer. I’m still learning, and this project is helping me improve my Django skills.

Thank you in advance!

The django-select2 package is very useful for cases like this.

Look to see how the Django admin does this. The “+” icon on the admin page opens a new window for a “create object” page, passing any parameters as necessary.

I’d be surprised if this doesn’t throw an error. You can’t reuse the related_name on two different fields.

1 Like

Hi KenWhitesell,

Thank you so much for the help you provided in your recent message. Your guidance was invaluable, and I wanted to let you know that I’ve corrected the error in my Django model. The fields are now correctly set up as follows:
father = models.ForeignKey(‘self’, on_delete=models.SET_NULL, null=True, blank=True, related_name=‘children_of_father’)
mother = models.ForeignKey(‘self’, on_delete=models.SET_NULL, null=True, blank=True, related_name=‘children_of_mother’)
Also, thank you for the suggestion regarding the Django admin’s use of the “+” icon to create new objects conveniently. However, I’m not quite sure how to implement this functionality in my custom form. Could you possibly provide some more detailed guidance or point me towards any resources or examples that show how to set this up?

Thank you once again for your help!

Best regards,

The source code in the admin is the best example.

If you bring up an admin page, you’ll see the “+” icon. If you look at the html associated with that icon, you’ll see it’s an <a ..> element to open up the new window for an admin page form.

1 Like

Thank you for pointing me towards the Django admin source code as an example. I’ll take a closer look at the admin page and study the HTML associated with the “+” icon to better understand how it opens a new window for an admin page form.
thanks a lot