Problem extending the Django.db.models.Manege class

Asked

Viewed 32 times

3

I extended the class django.db.models.Manager to pick up the records that are públicos and by doing so I can no longer list all the calling records Product.objects.all()

Even in my administration area, only public status records are listed. Private status records are hidden.

my mobile is

STATUS_PUBLISH = (
    ('público', 'pùblico'),
    ('privado', 'Privado'),
)

class ProductManager(models.Manager):
    def get_queryset(self):
        return super().get_queryset().filter(status_publish='público')


class Product(TimeStampedModel):

    name = models.CharField(max_length=255, blank=False, null=False, unique=True)
    slug = models.SlugField(max_length=255, blank=False, null=False)
    description = models.TextField(blank=True, null=True)
    status_publish = models.CharField(max_length=10, choices=STATUS_PUBLISH, default='público')
    tags = TaggableManager(blank=True, help_text='Separe as categorias com vírgula')

    publish = ProductManager()
    objects = models.Manager()

    class Meta:
        ordering = ('name',)

    def __str__(self):
        return self.name

    def get_absolute_url(self):
        return reverse("gallery:product_update", kwargs={"slug": self.slug})

    def save(self, *args, **kwargs):
        if not self.slug:
            self.slug = slugify(self.name)
        return super(Product, self).save(*args, **kwargs)
  • "Even in my area of admin only appear the registration with 'public status`". This statement you say that even saving the private option, the record changes to the public, or only those that are saved as public are presented q?

  • This, only those that are saved as 'public' are displayed. Those that are 'private' do not appear.

1 answer

3

I do not understand very well what you want to do, but if you want to list everything, just do not filter in Manager

class ProductManager(models.Manager):
    def get_queryset(self):
        return super().get_queryset().all()

if you want to list separately, you can do 2 managers (don’t know if manager plural writes =P)

class ProductPublicoManager(models.Manager):
    def get_queryset(self):
        return super().get_queryset().filter(status_publish='público')


class ProductPrivadoManager(models.Manager):
    def get_queryset(self):
        return super().get_queryset().filter(status_publish='privado')



class Product(TimeStampedModel):
    
    [...todo o código]

    publico = ProductPublicoManager()
    privado = ProductPrivadoManager()
    objects = models.Manager()
    
    [... resto do código]

  • Ok! Type. I just created the manager so I don’t have to write all the time Product.objects.filter(status_publish='público') and by writing this maneger, I can no longer list all products.

  • I don’t know if you tested it, (so it’s just supposed to be redundant) but the order you put the Manager influences the presentation of the administrative area. If you objects = models.Manager() before the publish = ProductManager() you would have another result.

Browser other questions tagged

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