How to filter form options by foreign key? - Django Admin

Asked

Viewed 31 times

-1

I need to show only objects that have the same foreign key company in Django in the admin view. What I got was to show only the related objects using get_queryset, but in the form fields appear all records.

My code:

models.py

class Company(models.Model):
    name = models.CharField(max_length=64)
    cnpj = models.CharField(max_length=14)
    active = models.BooleanField(default=False)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(null=True, blank=True)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name_plural = "Companies"


class User(models.Model):
    user = models.OneToOneField(BaseUser, on_delete=models.CASCADE)
    company = models.ForeignKey(Company, on_delete=models.CASCADE)
    department = models.ForeignKey(Department, on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(null=True, blank=True)

    def __str__(self):
        return f"{self.user.first_name} {self.user.last_name}"


class Category(models.Model):
    name = models.CharField(max_length=32)
    company = models.ForeignKey(Company, on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(null=True, blank=True)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name_plural = "Categories"

class Resource(models.Model):
    name = models.CharField(max_length=64)
    model = models.CharField(max_length=64)
    location = models.CharField(max_length=64)
    manufacturer = models.CharField(max_length=64)
    provider = models.CharField(max_length=64)
    serial = models.CharField(max_length=32)
    user = models.ForeignKey(
        User, null=True, blank=True, on_delete=models.CASCADE)
    category = models.ForeignKey(Category, on_delete=models.CASCADE)
    company = models.ForeignKey(Company, on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(null=True, blank=True)

    def __str__(self):
        return self.name

admin.py

@admin.register(Resource)
class ResourceAdmin(admin.ModelAdmin):
    list_display = (
        'name',
        'model',
        'location',
        'manufacturer',
        'provider',
        'serial',
        'user', # Campo que precisa ser filtrado pela chave estrangeira de company
        'category', # Campo que precisa ser filtrado pela chave estrangeira de company
        'created_at'
    )

    def get_queryset(self, request):
        user = User.objects.get(user=request.user)
        return Resource.objects.filter(company=user.company)

1 answer

1

Use two underscores to reference the foreign key (more on the subject here):

def get_queryset(self, request):
    return Resource.objects.filter(user__company=request.user.company)
  • @Ederlima, please consider the possibility of accepting the answer if it has been appropriate to resolve your question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.