N
N
Nikita Roschenko2015-08-18 16:25:51
Django
Nikita Roschenko, 2015-08-18 16:25:51

How to tag model in django instead of deleting it?

Hello, we have two models:

class Account(models.Model):
    login = models.CharField(max_length=255)
    password = models.CharField(max_length=255)

class Photo(models.Model):
    account = models.ForeignKey(Account)
    photo_name = models.CharField(max_length=255)
    photo_url = models.CharField(max_length=255)
    delete  = models.IntegerField(choices=((0, 'no'), (1, 'yes')), default=0)
    
    def delete(self, *args, **kwargs):
        self.delete = 1
        self.save()

And I would like that when deleting a Photo model, it would be marked as deleted and not displayed in the admin panel, what is the best way to do this?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey K, 2015-08-18
@Avillions

class PhotoAdmin(admin.ModelAdmin):
    def get_queryset(self, request):
        queryset = super(PhotoAdmin, self).get_queryset(request)
   
        return queryset.filter(delete=0)

S
sim3x, 2015-08-18
@sim3x

is_deleted = models.BooleanField(default=False)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question