0

Before adding a person in a event, I would like to search it.

  • If the person exists, add it.
  • If not, create it and add it.

I have tried many times with different ways in admin.py, but I am not able to get it.

Could someone help me with the admin.py?

models.py

class Person (models.Model):
    dni= models.CharField(primary_key = True, max_length=9 )
    name= models.CharField(max_length = 30)
    surname1 = models.CharField(max_length = 30)
    surname2 = models.CharField(max_length = 30)
    table = models.PositiveIntegerField(default=0)
    def __str__(self):
        return self.dni
    class Meta:
        ordering = ('dni',)

class Event (models.Model):
    name = models.CharField(max_length = 100)
    people= models.ManyToManyField(Person,blank = True, default = None)
    date = models.DateField('date event')
    category = models.CharField (choices = TYPE_EVENT, max_length = 1)

    def __str__(self):
        return self.name
    class Meta:
        ordering = ('name',)

Currently all persons appear in the box. But its not correct. enter image description here

I would like to get something like this (link) but I didn't get the solution :S.

Bidhan Majhi
  • 1,320
  • 1
  • 12
  • 25
Yari
  • 867
  • 3
  • 9
  • 13
  • You can try out search_field in ModelAdmin, checkout https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.search_fields – Bidhan Majhi Dec 21 '18 at 05:15
  • Thak you! I figured out it using `filter_horizontal = ('people',)` – Yari Dec 23 '18 at 16:37

0 Answers0