Admin

30 views
Skip to first unread message

Flavio Munita

unread,
May 5, 2023, 12:48:39 PM5/5/23
to Django users
Hi, I'm new to django, maybe my question is stupid, but I can't find how to do it. I have the following:

app lineasinv
Model

class Linea(models.Model):
    nombrelinea = models.TextField()
    area = models.ManyToManyField(Granarea)
    grupo = models.ManyToManyField(Grupo)
    descripcionlinea = RichTextField(blank = True)
    imagen = models.ImageField(upload_to='investigacion/lineas/', blank=True)
    admins = models.ManyToManyField(User)
    created = models.DateTimeField(auto_now_add=True, editable=False)
    updated = models.DateTimeField(auto_now_add=True, editable=False)
    state = models.IntegerField(choices=_STATES, default=0)

    class Meta:
        ordering = ['nombrelinea']

    def __str__(self):
        return str(self.nombrelinea)

       
app userprofile
model
class UserProfile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='UserProfile')
    avatar = models.ImageField(upload_to='userprofile/', blank=True)
    lineasinv = models.ManyToManyField(Linea)
    autornames = ArrayField(models.CharField(max_length=150, blank=True), blank=True, null=True)
    state = models.IntegerField(choices=_STATES, default=0)

    class Meta:
        ordering = ['user']

    def __str__(self):
        return str(self.user)

       
Lineasinv admin.py contains the following

from django.contrib import admin
from django.contrib.auth import get_user_model
from userprofile.models import UserProfile
from .models import Linea

user = get_user_model()

# Register your models here.
class LineaAdmin(admin.ModelAdmin):
    list_display = ['nombrelinea']

    def get_queryset(self, request):
        qs = super(LineaAdmin, self).get_queryset(request)
        for li in UserProfile.objects.all():
            if li.user == request.user:
                return qs.filter(li.user_id__in == request.user)


    if request.user.is_superuser:
        return qs      

admin.site.register(Linea, LineaAdmin)


What I want to do is that when a user enters the admin pannel, it shows only the linesinv that it has enabled according to userprofile, linesasinv's admin.py is not working.

Thank you so much !
Flavio

Omkkar Potddar

unread,
May 5, 2023, 12:57:28 PM5/5/23
to django...@googlegroups.com
Are you create super user.. And register your model in admin.. Please check first

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f43e44aa-9c2e-475a-9b3c-15e352c35986n%40googlegroups.com.

Flavio Munita

unread,
May 5, 2023, 1:33:59 PM5/5/23
to Django users
Hi Omkkar,

Thank you very much, I have already solved the problem.
Best regards,

 Flavio
Reply all
Reply to author
Forward
0 new messages