Pyrodius |
A Django Model Manager for Soft Deleting Records and How to Customize the Django Admin
You can do this.
from somewhere import SoftDeleteManager
class NewManager(SoftDeleteManager):
'''new stuff'''
and in the model
objects = NewManager()
A Django Model Manager for Soft Deleting Records and How to Customize the Django Admin
I felt the same way when I started especially since I didn't know any python yet. And now I'm almost as comfortable as I was with php. I'm glad I moved on from php. It's good to not be a single language developer.
A Django Model Manager for Soft Deleting Records and How to Customize the Django Admin
Yeah, I thought I'd write that post since before using this method, I was checking the flag each time I wanted a set of records. And I was doing that for a bunch of models. Now I can apply these managers to any new model that I create and I don't need to worry about it anymore.
A Django Model Manager for Soft Deleting Records and How to Customize the Django Admin
Sometimes it’s good to hide things instead of deleting them. Users may accidentally delete something and this way there will be an extra backup. The way I’ve been doing this is I set a flag in the database, deleted = 1. I wrote this code to automatically hide records from django if they are flagged.
Django allows developers to create model managers that can change how the models work. The code below was written to return only the undeleted records by default. I added two new methods in case I need to get some of the deleted records.
from django.db import models class SoftDeleteManager(models.Manager): ''' Use this manager to get objects that have a deleted field ''' def get_query_set(self): return super(SoftDeleteManager, self).get_query_set().filter(deleted=False) def all_with_deleted(self): return super(SoftDeleteManager, self).get_query_set() def deleted_set(self): return super(SoftDeleteManager, self).get_query_set().filter(deleted=True)This is usable by many models by adding this line to the model (it needs a deleted field) objects = SoftDeleteManager()
This will hide deleted records from django completely, even the django admin and even if you specify the id directly. The only way to find it is through the database itself or an app like phpMyAdmin. This might be good for some cases, but I went a step further to make it possible to undelete things in the django admin.
Django has a lot of customization options for the admin interface ( this article has some more info on customizing the django admin). I wanted the queryset to be different in the admin, so I created a ModelAdmin to customize what is displayed. First I set it up to show a few more columns than just __unicode__ on the list of items and added a filter to help easily separate the deleted from the active.
from django.contrib import admin class SoftDeleteAdmin(admin.ModelAdmin): list_display = ('id', '__unicode__', 'deleted',) list_filter = ('deleted',) # this requires __unicode__ to be defined in your modelThis can also be used by many models by adding this at the bottom of the models.py file:
from django.contrib import admin from wherever import SoftDeleteAdmin admin.site.register(MyModel, SoftDeleteAdmin)The next thing to do was override the queryset method in the default ModelAdmin. I copied the code from the django source and changed it from using get_query_set to make it use all_with_deleted() which was a method added to the ModelManager. The following code was added to SoftDeleteAdmin.
def queryset(self, request): """ Returns a QuerySet of all model instances that can be edited by the admin site. This is used by changelist_view. """ # Default: qs = self.model._default_manager.get_query_set() qs = self.model._default_manager.all_with_deleted() # TODO: this should be handled by some parameter to the ChangeList. ordering = self.ordering or () # otherwise we might try to *None, which is bad ;) if ordering: qs = qs.order_by(*ordering) return qsThe list of objects in the admin will start to look like this.
A screenshot of the django admin interface
They are showing up there now, but won’t be editable yet because django is using get_query_set to find them. There are two methods I added to SoftDeleteManager so that django can find the deleted records.
def get(self, *args, **kwargs): ''' if a specific record was requested, return it even if it's deleted ''' return self.all_with_deleted().get(*args, **kwargs) def filter(self, *args, **kwargs): ''' if pk was specified as a kwarg, return even if it's deleted ''' if 'pk' in kwargs: return self.all_with_deleted().filter(*args, **kwargs) return self.get_query_set().filter(*args, **kwargs)With those updated methods, django will be able to find records if the primary key is specified, not only in the admin section, but everywhere in the project. Lists of objects will only return deleted records in the admin section still.
This code can be applied to a bunch of models and easily allow soft deletes of records to prevent loss of accidentally deleted objects.
addthis_url = 'http%3A%2F%2Fcodespatter.com%2F2009%2F07%2F01%2Fdjango-model-manager-soft-delete-how-to-customize-admin%2F'; addthis_title = 'A+Django+Model+Manager+for+Soft+Deleting+Records+and+How+to+Customize+the+Django+Admin'; addthis_pub = 'gallard';Django Single Sign On or a Solution to Multi-domain Cookies
I’ve been working on a project for a while and it has recently started to expand to an additional domain name. The domains will be using the same user base and I want to make it simple for users to be logged in at both applications. With a little research I dug up a few options I could go with. There is a redirect option, a javascript option, or a single sign on option.
With the redirect option I could redirect users to the main domain, check for cookies, and redirect them back so that they could get new cookies for the additional domain. The downside to this method is it will increase traffic for every pageload from a new visitor even if they will never need to log in. And since the sites this was for will have pages being viewed many more times than there will be logged in users, it wasn’t worth all of the extra traffic. It might be possible to minimize this traffic by only redirecting on login pages, but if the login form is at the top of all pages then it doesn’t help much.
Facebook uses a javascript method on all of the sites where you see facebook connect so you can use your facebook credentials to comment on blogs and other things. This method may be fine for their case, but again it will cause the extra traffic since the javascript is still connecting to the main server to get cookie info. I also don’t want to rely on javascript for my sessions.
I wanted a solution where it would only keep users logged in when they needed to be kept logged in. One way of knowing if they need to be kept logged in is: they are on one domain and click a link to go over to the other domain. Using a single-sign-on link to the other domain, the user would stay logged in at the new domain. The only use case that this doesn’t account for is someone is logged in at one domain and then types the other domain into the address bar. However that is a minimal case and I think the sso link will be the best way to keep users logged in most of the time and keep the overhead down.
I plan on open sourcing the django sso code so that other people can use it in their projects. It will allow a django site to accept single sign on requests and it will also help to create single sign on links to other sites. Both ends of the process don’t need to be a django site since it should work with other applications that use this type of process to authenticate users.
I’ll write a post on here about how to use the code once I get it set up at google code so if you are interested in that, you should probably subscribe to the rss so you don’t miss it.
addthis_url = 'http%3A%2F%2Fcodespatter.com%2F2009%2F06%2F18%2Fdjango-single-sign-on-or-a-solution-to-multi-domain-cookies%2F'; addthis_title = 'Django+Single+Sign+On+or+a+Solution+to+Multi-domain+Cookies'; addthis_pub = 'gallard';Why you should make it easy for users to quit your product
I just went through the whole xbox live cancel thing too. No place on the site or in the console to quit. Tech support e-mail would always say call them. The person on the phone was able to take care of everything and even mentioned billing.microsoft.com would allow you to stop the auto renew. I didn't try that out since I was already on the phone. I don't know why the e-mail support responses left that out. If that site works for someone, it would be nice to know.
Ask HN: What web/desktop apps do you use to manage your startup?
This wiki was created to list what tools are used by which startups. http://startuptools.pbworks.com/
Doing more with the Django admin
I like this: >Our apologies… > >The page you requested cannot be displayed
Ask HN: Feedback on webapp, online meetings for group collaboration
It's good that you have a video showing the features, but it could use some more audio. The silent parts need a voiceover. It looks like there is enough stuff to talk about to fill up the whole time.
Ask HN: Feedback on webapp, online meetings for group collaboration
It's good that you have a video showing the features, but it could use some more audio. The silent parts need a voiceover. It looks like there is enough stuff to talk about to fill up the whole time.
Safe truncation of HTML
This is useful. I just tried it with this
test = " go to http://ole-laursen.blogspot.com/2009/05/safe-truncation-of-html.html "
{{test|urlize|truncatehtml:25}}
And it worked how expected.
Safe truncation of HTML
This is useful. I just tried it with this
test = " go to http://ole-laursen.blogspot.com/2009/05/safe-truncation-of-html.html "
{{test|urlize|truncatehtml:25}}
And it worked how expected.
Quick Thumbnails in Django
I updated this post. I was thinking the uploaded file was deleted after using it, but I just needed to reset the file. Django's InMemoryUploadedFile uses StringIO. Doing file.seek(0)
will reset the StringIO file to be ready to create another thumbnail.
Quick Thumbnails in Django
I updated this post. I was thinking the uploaded file was deleted after using it, but I just needed to reset the file. Django's InMemoryUploadedFile uses StringIO. Doing file.seek(0)
will reset the StringIO file to be ready to create another thumbnail.
Ask YC: Cheap alternatives to Aeron chairs?
In addition to this question it would be good to know where to go to try out all of these chairs that are mentioned. Most places I've been only have the cheap ones out for trial.
Ask YC: Cheap alternatives to Aeron chairs?
In addition to this question it would be good to know where to go to try out all of these chairs that are mentioned. Most places I've been only have the cheap ones out for trial.
Ask HN: Review a webpage to Kindle conversion app
It would be nice to know more about how the website will be sent to my kindle before giving out my kindle email address.
Ask HN: Review a webpage to Kindle conversion app
It would be nice to know more about how the website will be sent to my kindle before giving out my kindle email address.
How to Speed up Your Django Sites
I just installed django-memcache-status to see the usage of memcache in the django admin interface. It looks pretty good and it's easy to install. Just download it and add it to installed apps. http://github.com/bartTC/django-memcache-status/
How to Speed up Your Django Sites
I just installed django-memcache-status to see the usage of memcache in the django admin interface. It looks pretty good and it's easy to install. Just download it and add it to installed apps. http://github.com/bartTC/django-memcache-status/
