Snippet List
based on #2020
This one is even more generic than the previous generic ones since you can specify in fields any attribute you will give to the admin interface and not just fields from the model.
You can for example directly export the list_display as a list of fields, including look-ups for related attributes that you may have defined in a function inside the Admin Class
- admin
- csv
- related
- admin-actions
Here's the python code to produce an admin related widget with the edit and delete link also.
* [RelatedFieldWidgetWrapper](http://djangosnippets.org/snippets/2562)
* [related-widget-wrapper.html](http://djangosnippets.org/snippets/2563)
* [related-widget-wrapper.js](http://djangosnippets.org/snippets/2564)
* [RelatedWidgetWrapperAdmin](http://djangosnippets.org/snippets/2565)
- admin
- related
- widget
- wrapper
Here's the python code to produce an admin related widget with the edit and delete link also.
* [RelatedFieldWidgetWrapper](http://djangosnippets.org/snippets/2562)
* [related-widget-wrapper.html](http://djangosnippets.org/snippets/2563)
* [related-widget-wrapper.js](http://djangosnippets.org/snippets/2564)
* [RelatedWidgetWrapperAdmin](http://djangosnippets.org/snippets/2565)
- admin
- related
- widget
- wrapper
Here's the python code to produce an admin related widget with the edit and delete link also.
* [RelatedFieldWidgetWrapper](http://djangosnippets.org/snippets/2562)
* [related-widget-wrapper.html](http://djangosnippets.org/snippets/2563)
* [related-widget-wrapper.js](http://djangosnippets.org/snippets/2564)
* [RelatedWidgetWrapperAdmin](http://djangosnippets.org/snippets/2565)
- admin
- related
- widget
- wrapper
Here's the python code to produce an admin related widget with the edit and delete link also.
* [RelatedFieldWidgetWrapper](http://djangosnippets.org/snippets/2562)
* [related-widget-wrapper.html](http://djangosnippets.org/snippets/2563)
* [related-widget-wrapper.js](http://djangosnippets.org/snippets/2564)
* [RelatedWidgetWrapperAdmin](http://djangosnippets.org/snippets/2565)
- admin
- related
- widget
- wrapper
When deleting objects in Django's admin interface, it lists other objects which would be deleted and asks for confirmation. This snippet does the same programmatically.
The snippet works in Django 1.3 (more specifically, revision 14507 or later). It uses Django internals which are not a part of the public API, so this might not work with future versions.
Usage:
`polls/models.py`:
from django.db import models
class Poll(models.Model):
question = models.CharField(max_length=200)
def __unicode__(self):
return self.question
class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice = models.CharField(max_length=200)
def __unicode__(self):
return '%s %s' % (self.poll, self.choice)
`$ ./manage.py shell`
>>> from polls.models import Poll, Choice
>>> from datetime import datetime
>>> from pprint import pprint
>>> poll1 = Poll.objects.create(question='Me?')
>>> Choice.objects.create(poll=poll1, choice='Yes')
>>> Choice.objects.create(poll=poll1, choice='No')
>>> poll2 = Poll.objects.create(question='Really?')
>>> Choice.objects.create(poll=poll2, choice='Yes')
>>> Choice.objects.create(poll=poll2, choice='No')
>>> pprint(get_related(Poll.objects.all()))
{<class 'polls.models.Poll'>: [<Poll: Me?>, <Poll: Really?>],
<class 'polls.models.Choice'>: [<Choice: Me? Yes>,
<Choice: Me? No>,
<Choice: Really? Yes>,
<Choice: Really? No>]}
Generic function to merge model instances. Useful when you need to merge duplicate models together, e.g. for users.
Based on http://djangosnippets.org/snippets/382/, with several enhancements:
* *Type checking*: only Model subclasses can be used and testing that all instances are of same model class
* *Handles symmetrical many-to-may*: original snippet failed in that case
* *Filling up blank attrs of original when duplicate has it filled*
* *Prepared to use outside of command-line*
- django
- model
- generic
- related
- merge
This will allow you to attach HTML multipart emails (HTML/text) and use inline images.
In my example, I'm attaching an image that's stored as an 'attachment' to an 'event.' The file name of the attachment is called "inline.jpg" and I'm referencing it in my HTML message. I'm also attaching a VCAL file (.ics) file that has some information about an associated event.
- email
- html
- related
- mixed
- images
- inline
- multipart
Django tagging by default doesn't provide a templatetag to get the related objects for another object. Even though this is implemented as a model. Still, one can use the existing templatetags to achieve the same outcome.
Of course, writing a custom templatetag would be more efficient in terms of database access.
- templatetag
- tagging
- related
- django-tagging
Some times I want to change the `owner` of an object to another user - problem is the object often has a lot of other objects pointing to them - I also want to update those fields.
This is a generic snippet for doing just that!
For instance:
change_owner(obj, new_owner_id):
return update_related_field(obj, new_owner_id, field="user")
Use this function to merge model objects (i.e. Users, Organizations, Polls, Etc.) and migrate all of the related fields from the alias objects the primary object.
Usage:
from django.contrib.auth.models import User
primary_user = User.objects.get(email='[email protected]')
duplicate_user = User.objects.get(email='[email protected]')
merge_model_objects(primary_user, duplicate_user)
- django
- fields
- model
- generic
- related
- merge
- duplicates
- genericforeignkey
13 snippets posted so far.