djangosnippets.org: Latest snippets tagged with 'foreignkey'http://djangosnippets.org/tags/foreignkey/2018-10-12T11:13:54.203739-05:00Hand over model instance relations to another instance of the same model
2018-10-12T11:13:54.203739-05:00karolyihttp://djangosnippets.org/snippets/10654/<p>If you have a model instance you want to merge into another, it's handy to hand over all the relations into the model you want to merge into, so the deletion won't trigger cascading deletions from other tables.</p>
<p>You can pass an <code>Iterable</code> of the same objects (a.k.a <code>QuerySet</code>) …</p>
Freely redistributabledjango admin filter for GenericForeignKey field
2016-04-21T04:27:32.027802-05:00dkoldyaevhttp://djangosnippets.org/snippets/10564/<p>Simple filter for django ModelAdmin</p>
<p>How use:</p>
<pre><code>#models.py
class ObjectWithGenericForeignKey(model.Model):
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object= GenericForeignKey('content_type', 'object_id', for_concrete_model=False)
#admin.py
class CommentAdmin(admin.ModelAdmin):
list_filter = (get_generic_foreign_key_filter(u'Filter title'),)
</code></pre>
Freely redistributableForeignKey filterspec
2010-09-13T09:32:31.406677-05:00luc_jhttp://djangosnippets.org/snippets/2194/<p>Unfortunately, it is not possible currently to use foreign keys in list filter of the admin website. list_filter=['city__country'] doesn't work.</p>
<p>This filter spec tries to workaround this problem.</p>
<p>It is also possible to have 2 filters for a foreign-key field but it requires to add a dummy field to the …</p>
Freely redistributableImproved generic foreign key manager 2
2009-10-25T16:16:18.154445-05:00Nomalzhttp://djangosnippets.org/snippets/1773/<p>This is an improvement on <a href="http://www.djangosnippets.org/snippets/1079/">snippet 1079</a>. Please read its description and <a href="http://zerokspot.com/weblog/2008/08/13/genericforeignkeys-with-less-queries/">this blog post</a> for any information.
</p>
<p>This is a manager for handling generic foreign key. Generic foreign objects of the same type are fetched together in order to reduce the number of SQL queries.
</p>
<p>To use, just …</p>
Freely redistributableURL models
2009-10-09T18:11:07.721923-05:00divermanhttp://djangosnippets.org/snippets/1755/<p>You can use <code>UrlModel</code> to provide URL functionality to any instance of any model and any language (language support can be removed from this). Each model must have own view method, that returns HttpResponse. I was inspired by Flatpages. It is useful for small sites and static pages.</p>
<pre><code>`class Page(UrlModel): …</code></pre>
Freely redistributableUser manager
2009-07-12T15:47:16.350568-05:00divermanhttp://djangosnippets.org/snippets/1626/<p>If you have a model with foreign key to User, you can use this manager to show (i.e. in admin interface) only objects, that are related to currently logged-in user. Superuser sees all objects, not only his.</p>
<p>Requires: <a href="http://code.djangoproject.com/wiki/CookBookThreadlocalsAndUser">ThreadlocalsMiddleware</a></p>
Freely redistributableanother UserForeignKey
2009-07-12T15:38:23.676306-05:00divermanhttp://djangosnippets.org/snippets/1625/<p>This is another foreign key to User model. User is automatically associated before save. </p>
<p>Requires: <a href="http://code.djangoproject.com/wiki/CookBookThreadlocalsAndUser">ThreadlocalsMiddleware</a></p>
<p>Inspired by: <a href="http://www.djangosnippets.org/snippets/509/">snippet 509</a></p>
Freely redistributableFiltering foreignkey fields in django admin
2009-06-09T07:10:43.554405-05:00forgemshttp://djangosnippets.org/snippets/1558/<p>Sometimes you need to filter foreignkey choices in admin interface, to objects created by user or meeting some other condition. In django 1.0 there is no formfield_for_foreignkey method in ModelAdmin class so we have to make a workaround. Here is the solution I have found to be the easiest for …</p>
Freely redistributablePrefill ForeignKey caches
2008-10-08T18:39:09.967948-05:00insinhttp://djangosnippets.org/snippets/1117/<p>Provides an efficient means of looking up multiple related model instances for a range of objects by pre-filling the cache attribute used by <code>SingleRelatedObjectDescriptor</code> with either complete model instances or a dict containing only specified fields, looking up all required data with a single query.</p>
<p>Example usage:</p>
<pre><code>C:\django_projects\soclone>django-admin.py shell
>>> …</code></pre>
Freely redistributableimproved generic foreign key manager
2008-09-22T16:03:06.179379-05:00carljmhttp://djangosnippets.org/snippets/1079/<p>This is an improvement on <a href="http://www.djangosnippets.org/snippets/984/">snippet 984</a>. Read it's description and <a href="http://zerokspot.com/weblog/2008/08/13/genericforeignkeys-with-less-queries/">this blog post</a> for good explanations of the problem this solves.
</p>
<p>Unlike snippet 984, this version is able to handle multiple generic foreign keys, generic foreign keys with nonstandard ct_field and fk_field names, and avoids unnecessary lookups to …</p>
Freely redistributableManager method for limiting GenericForeignKey queries
2008-08-15T14:50:34.506282-05:00zerokhttp://djangosnippets.org/snippets/984/<p>This is a simple manager that offers one additional method called <code>relate</code>, which fetches generic foreign keys (as referenced by <code>content_type</code> and <code>object_id</code> fields) without requiring one additional query for each contained element. </p>
<p>Basically, when working with generic foreign keys (and esp. in the usecase of having something like …</p>
Freely redistributableEditInline for GenericForiegnKey II
2008-06-29T19:56:02.801619-05:00paltmanhttp://djangosnippets.org/snippets/832/<p>This is an update to <a href="http://www.djangosnippets.org/snippets/765/">snippet 765</a> as I was having trouble getting it to work on branches/newforms-admin @ r7771.</p>
<p>There are just a few minor changes to the previous snippet, all simple stuff. I went ahead an added can_delete and can_order options that the previous snippet didn't include.</p>
<p><a href="http://paltman.com/2008/06/29/edit-inline-support-for-generic-relations/">More …</a></p>
Freely redistributableEditInline for GenericForeignKey
2008-05-20T15:19:03.645247-05:00kinghttp://djangosnippets.org/snippets/765/<p>A simple InlineModelAdmin class that enables you to edit models that are bound by the instance via a generic foreign key (<code>content_type</code>, <code>object_id</code> pair)</p>
<p>Use like:</p>
<pre><code>class PlacementInlineOptions( generic.GenericTabularInline ):
model = Placement
extra = 2
ct_field_name = 'target_ct'
id_field_name = 'target_id'
</code></pre>
<p>Can be also found at #4667</p>
Freely redistributableUserForeignKey
2008-02-22T20:30:32.264523-06:00hawkeyehttp://djangosnippets.org/snippets/612/<p>Many models are tightly coupled to the default Django <code>User</code> model (<code>django.contrib.auth.models.User</code>).</p>
<p>Sometimes this user model just doesn't fit everyone's needs. By using <code>UserForeignKey</code> it is possible to make the <code>User</code> model configurable, encouraging loose coupling.</p>
<p>Additionally, this can help to prevent circular imports between <code>User</code> and another …</p>
Freely redistributableCached lookup model mixin
2008-01-22T15:55:55.663896-06:00isagalaevhttp://djangosnippets.org/snippets/560/<p>This mixin is intended for small lookup-style models that contain mostly static data and referenced by foreign keys from many other places. A good example is a list of Payment options in an e-shop that is referenced from Orders and is hitting database <code>order.payment</code> at least one time for an …</p>
Freely redistributable