<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Alexis Petrounias</title>
	<atom:link href="http://www.petrounias.org/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.petrounias.org</link>
	<description>Information Systems Engineer, Athens and Thessaloniki, Greece</description>
	<lastBuildDate>Sun, 24 Feb 2013 17:29:10 +0000</lastBuildDate>
	<language>el-GR</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>(English) Django application import and missed class_prepared signals.</title>
		<link>http://www.petrounias.org/el/articles/2011/08/11/django-application-import-and-missed-class_prepared-signals/</link>
		<comments>http://www.petrounias.org/el/articles/2011/08/11/django-application-import-and-missed-class_prepared-signals/#comments</comments>
		<pubDate>Thu, 11 Aug 2011 10:17:43 +0000</pubDate>
		<dc:creator>Alexis Petrounias</dc:creator>
				<category><![CDATA[Django]]></category>

		<guid isPermaLink="false">http://www.petrounias.org/?p=368</guid>
		<description><![CDATA[Το περιεχόμενο δεν είναι διαθέσιμο στην Ελληνική γλώσσα.]]></description>
				<content:encoded><![CDATA[<p>Το περιεχόμενο δεν είναι διαθέσιμο στην Ελληνική γλώσσα.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.petrounias.org/el/articles/2011/08/11/django-application-import-and-missed-class_prepared-signals/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>(English) Propagating field changes in Django Model instance aliases</title>
		<link>http://www.petrounias.org/el/articles/2010/04/05/propagating-field-changes-in-django-model-instance-aliases/</link>
		<comments>http://www.petrounias.org/el/articles/2010/04/05/propagating-field-changes-in-django-model-instance-aliases/#comments</comments>
		<pubDate>Mon, 05 Apr 2010 10:11:23 +0000</pubDate>
		<dc:creator>Alexis Petrounias</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[alias]]></category>

		<guid isPermaLink="false">http://www.petrounias.org/?p=279</guid>
		<description><![CDATA[]]></description>
				<content:encoded><![CDATA[</p>
]]></content:encoded>
			<wfw:commentRss>http://www.petrounias.org/el/articles/2010/04/05/propagating-field-changes-in-django-model-instance-aliases/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Capturing invoking method information in Python</title>
		<link>http://www.petrounias.org/el/articles/2010/03/16/capturing-invoking-method-information-in-python/</link>
		<comments>http://www.petrounias.org/el/articles/2010/03/16/capturing-invoking-method-information-in-python/#comments</comments>
		<pubDate>Tue, 16 Mar 2010 11:04:37 +0000</pubDate>
		<dc:creator>Alexis Petrounias</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[inspect]]></category>
		<category><![CDATA[invoker]]></category>
		<category><![CDATA[method]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[stack frame]]></category>

		<guid isPermaLink="false">http://www.petrounias.org/?p=296</guid>
		<description><![CDATA[Το περιεχόμενο δεν είναι διαθέσιμο στην Ελληνική γλώσσα.]]></description>
				<content:encoded><![CDATA[<p>Το περιεχόμενο δεν είναι διαθέσιμο στην Ελληνική γλώσσα.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.petrounias.org/el/articles/2010/03/16/capturing-invoking-method-information-in-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ordering on a field in the &#8216;through&#8217; Model of a recursive ManyToMany relation in Django</title>
		<link>http://www.petrounias.org/el/articles/2010/02/06/ordering-on-a-field-in-the-through-model-of-a-recursive-manytomany-relation-in-django/</link>
		<comments>http://www.petrounias.org/el/articles/2010/02/06/ordering-on-a-field-in-the-through-model-of-a-recursive-manytomany-relation-in-django/#comments</comments>
		<pubDate>Sat, 06 Feb 2010 13:11:52 +0000</pubDate>
		<dc:creator>Alexis Petrounias</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[manytomany]]></category>
		<category><![CDATA[ordering]]></category>
		<category><![CDATA[recursive relation]]></category>
		<category><![CDATA[through]]></category>

		<guid isPermaLink="false">http://new.petrounias.org/?p=101</guid>
		<description><![CDATA[It is not possible to order a Model on a field of the Model acting as the intermediate &#8216;through&#8216; of a ManyToMany relation in Django, because queries will not return items in the correct order, and in addition will include duplicate items, even when using .distinct(). This problem is current as of Django 1.2 and [...]]]></description>
				<content:encoded><![CDATA[<p>It is not possible to order a <strong>Model</strong> on a field of the <strong>Model</strong> acting as the intermediate &#8216;<strong>through</strong>&#8216; of a ManyToMany relation in <a href="http://www.djangoproject.com/">Django</a>, because queries will not return items in the correct order, and in addition will include duplicate items, even when using <strong>.distinct()</strong>.</p>
<p><em>This problem is current as of Django 1.2 and has no generic solution as far as I know.</em></p>
<p><span id="more-101"></span>Assuming the following <strong>Model</strong>:</p>
<pre class="brush: py">class Category(models.Model):
    related = models.ManyToManyField('self', symmetrical = False, \
    through = 'CategoryRelation', null = True, blank = True)</pre>
<p>Assuming the following intermediate &#8216;<strong>through</strong>&#8216; relation:</p>
<pre class="brush: py">
class CategoryRelation(models.Model):
    source = models.ForeignKey('Category', null = False, blank = False, \
        related_name = 'relation_source')

    target = models.ForeignKey('Category', null = False, blank = False, \
        related_name = 'relation_target')

    order = models.PositiveIntegerField(_('order'), default = 0, \
        null = False, blank = True)
    class Meta:
        ordering = ( 'order', )
</pre>
<p>How can we obtain the <strong>Category</strong> objects related to a given <strong>Category</strong> while preserving their ordering? The following code will produce the correct <strong>Category</strong> objects, not in the correct order, and include duplicate entries in the Query Set even when using <strong>.distinct()</strong>:</p>
<pre class="brush: py">relations = CategoryRelation.objects.filter(source = self)

Category.objects.filter(relation_target__in = relations).order_by('related')</pre>
<p>The following works for ordering correctly, but does not leave out duplicate entries:</p>
<pre class="brush: py">relations = CategoryRelation.objects.filter(source = self)

Category.objects.filter(relation_target__in = relations).order_by('relation_target')</pre>
<p>Calling <strong>.distinct()</strong> will not make a difference, because <strong>.order_by(&#8230;)</strong> is applied by the ORM after the SQL <strong>SELECT DISTINCT</strong> clause is built. However, it is possible &#8211; in this case &#8211; to exploit the fact that the order is a positive integer field, and annotate each <strong>Category</strong> with the <strong>Min</strong> value of the order field of the <strong>relation_target</strong> field, and use this new annotation field for ordering:</p>
<pre class="brush: py">return Category.objects.filter(relation_target__in = relations).annotate(
    relation_target_order = models.Min('relation_target__order')).order_by(
        'relation_target_order')</pre>
<p>This is almost complete, but since the semantics of this query essentially make it unique, it would be wise to call <strong>.distinct()</strong> just to make sure the distinct flag is <em>True</em> so that later combinations with other distinct queries can take place:</p>
<pre class="brush: py">return Category.objects.filter(relation_target__in = relations).annotate(
    relation_target_order =models.Min('relation_target__order')).order_by(
        'relation_target_order').distinct()</pre>
<p>In this case <strong>.distinct()</strong> does not affect the query in the slightest, but ensures that <em>db/models/sql/query.py</em> method <strong>combine(self, rhs, connector)</strong> passes its assertion:</p>
<pre class="brush: py">assert self.distinct == rhs.distinct, \ ...</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.petrounias.org/el/articles/2010/02/06/ordering-on-a-field-in-the-through-model-of-a-recursive-manytomany-relation-in-django/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Duplicate items when ordering by Generic Relation in Django</title>
		<link>http://www.petrounias.org/el/articles/2010/01/27/duplicate-items-when-ordering-by-generic-relation-in-django/</link>
		<comments>http://www.petrounias.org/el/articles/2010/01/27/duplicate-items-when-ordering-by-generic-relation-in-django/#comments</comments>
		<pubDate>Wed, 27 Jan 2010 08:50:50 +0000</pubDate>
		<dc:creator>Alexis Petrounias</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[content types]]></category>
		<category><![CDATA[generic relation]]></category>
		<category><![CDATA[ordering]]></category>

		<guid isPermaLink="false">http:/?p=1</guid>
		<description><![CDATA[When the optional ordering = &#8230; attribute of a model&#8217;s Meta class contains a GenericRelation from the Content Types framework in Django, there is no way to eliminate duplicate items being returned, even when using .distinct() (since .order_by(&#8230;) is applied by the ORM only after the SQL SELECT DISTINCT clause is built). This problem is [...]]]></description>
				<content:encoded><![CDATA[<p>When the optional <strong>ordering = &#8230;</strong> attribute of a model&#8217;s <strong>Meta</strong> class contains a <strong>GenericRelation</strong> from the <a href="http://docs.djangoproject.com/en/dev/ref/contrib/contenttypes/">Content Types</a> framework in <a href="http://www.djangoproject.com/">Django</a>, there is no way to eliminate duplicate items being returned, even when using <strong>.distinct()</strong> (since <strong>.order_by(&#8230;)</strong> is applied by the ORM only after the SQL <strong>SELECT DISTINCT</strong> clause is built).</p>
<p><em>This problem is current as of Django 1.2 and has no generic solution as far as I know.</em></p>
<p><span id="more-1"></span>This problem will manifest when two or more different <strong>Models</strong>&#8216; instances are attached to instances of the <strong>Model</strong> with the <strong>Generic Foreign Key</strong>. In the following example, we have <strong>Model</strong> X which has a <strong>Generic Foreign Key</strong>, and two <strong>Models</strong> Y and Z which have <strong>Generic Relations</strong> to X and ordering based on these relations.</p>
<pre class="brush: py">from django.db.models import CharField

from django.contrib.contenttypes.models import ContentType

from django.contrib.contenttypes.generic import GenericForeignKey, GenericRelation

class X(Model):
    """ This Model has a unique name and is attached to another Model through a
        Generic relation.
    """

    # The unique name of this X.
    name = CharField(u'name', unique = True, max_length = 128, null = False)

    # The Content Type of the generic relation to the attached content.
    content_type = models.ForeignKey(ContentType, null = True, blank = True,
        related_name = 'x_contents')

    # The ID of the attached content.
    content_id = models.PositiveIntegerField(null = True, blank = True)

    # The attached content.
    content = GenericForeignKey('content_type', 'content_id')

    def __unicode__(self):
        """ The textual representation of this Model is its name.
        """

        return self.name

    class Meta:

        app_label = 'cmf'

        # Order by X's name.
        ordering = ['name', ]</pre>
<pre class="brush: py">class Y(Model):
    """ This Model acts as content for an X, and has a convenient reverse
        Generic Relation to X.
    """

    # The unique name of this Y.
    name = CharField(u'name', unique = True, max_length = 128, null = False)

    # The X this Y is attached to.
    xs = GenericRelation(X, content_type_field = 'content_type',
        object_id_field = 'content_id')

    def __unicode__(self):
        """ The textual representation of this Model is its name.
        """

        return self.name

    class Meta:

        # Order by X's name.
        ordering = ['xs__name', ]</pre>
<pre class="brush: py">class Z(Model):
    """ This Model acts as content for an X, and has a convenient reverse
        Generic Relation to X.
    """

    # The unique name of this Z.
    name = CharField(u'name', unique = True, max_length = 128, null = False)

    # The X this Z is attached to.
    xs = GenericRelation(X, content_type_field = 'content_type',
        object_id_field = 'content_id')

    def __unicode__(self):
        """ The textual representation of this Model is its name.
        """

        return self.name

    class Meta:

        app_label = 'cmf'

        # Order by X's name.
        ordering = ['xs__name', ]</pre>
<p>The following sequence of statements demonstrates this problem:</p>
<p>x1 = X.objects.create(name = &#8216;first x&#8217;)<br />
x2 = X.objects.create(name = &#8216;second x&#8217;)<br />
x3 = X.objects.create(name = &#8216;third x&#8217;)<br />
x4 = X.objects.create(name = &#8216;fourth x&#8217;)</p>
<p>y1 = Y.objects.create(name = &#8216;first y&#8217;)<br />
y2 = Y.objects.create(name = &#8216;second y&#8217;)</p>
<p>z1 = Z.objects.create(name = &#8216;first z&#8217;)<br />
z2 = Z.objects.create(name = &#8216;second z&#8217;)</p>
<p>x1.content = y1<br />
x1.save()</p>
<p>x2.content = z1<br />
x2.save()</p>
<p>x3.content = y2<br />
x3.save()</p>
<p>x4.content = z2<br />
x4.save()</p>
<p>X.objects.all()<br />
[&lt;X: first x&gt;, &lt;X: fourth x&gt;, &lt;X: second x&gt;, &lt;X: third x&gt;]</p>
<p>Y.objects.all()<br />
[&lt;Y: first y&gt;, &lt;Y: first y&gt;, &lt;Y: second y&gt;]</p>
<p>Notice that &#8216;first y&#8217; appears twice.</p>
<p>Y.objects.count()<br />
2</p>
<p>The SQL <strong>COUNT</strong> statement will not have any ordering applied, and so returns the correct value.</p>
<p>len(Y.objects.all())<br />
3</p>
]]></content:encoded>
			<wfw:commentRss>http://www.petrounias.org/el/articles/2010/01/27/duplicate-items-when-ordering-by-generic-relation-in-django/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
