Hi Omkkar,
Thank you very much, I have already solved the problem.
Best regards,
Flavio
On Friday, May 5, 2023 at 1:57:28 PM UTC-3 Omkkar Potddar wrote:
Are you create super user.. And register your model in admin.. Please check firstOn Fri, 5 May, 2023, 10:18 pm Flavio Munita, <flavio...@gmail.com> wrote: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
--
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.
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/1044afc7-1584-48e9-a352-6b95ee97def3n%40googlegroups.com.
No comments:
Post a Comment