Hi all,

I have the following 3 Models:

class Company(models.Model):
        name = models.CharField(maxlength=40)

class InfoType(models.Model):
        type = models.CharField(maxlength=40)

class MetaObject(models.Model):
        company = models.ManyToManyField(Company, null=True, blank=True)
        type    = models.ManyToManyField(InfoType)
        title   = models.CharField(maxlength=60)

I want to select now all objects of InfoType for Company belonging indirectly 
together by MetaObject.

I have implemented this into Company:

class Company(models.Model):
        ...
        def infotypes(self):
                metaobjects = self.metaobject_set
                if metaobjects.count() > 0:
                        return 
InfoType.objects.filter(metaobject__in=metaobjects.all()).distinct()


This solution will output the following SQL:

{'time': '0.042', 'sql': 'SELECT DISTINCT 
`core_infotype`.`id`,`core_infotype`.`type` FROM `core_infotype` LEFT OUTER 
JOIN `core_metaobject_type` AS `m2m_core_infotype__metaobject` ON 
`core_infotype`.`id` = `m2m_core_infotype__metaobject`.`infotype_id` WHERE 
(`m2m_core_infotype__metaobject`.`metaobject_id` IN 
(1,32,63,94,125,156,187,218,249,280,311,342,373,404,435,466,497,528,559,590,621,652,683,714,745,776,807,838,869,900,931,962,993,1024,
1055,1086,1117,1148,1179,1210,1241,1272,1303,1334,1365,1396,1427,1458,1489,1520,1551,1582,1613,1644,1675,1706,1737,1768,1799,1830,
1861,1892,1923,1954,1985,2016,2047,2078,2109,2140,2171,2202,2233,2264,2295,2326,2357,2388,2419,2450,2481,2512,2543,2574,2605,2636,
2667,2698,2729,2760,2791,2822,2853,2884,2915,2946,2977,3008,3039,3070,3101,3132,3163,3194,3225,3256,3287,3318,3349,3380,3411,3442,
3473,3504,3535,3566,3597,3628,3659,3690,3721,3752,3783,3814,3845,3876,3907,3938,3969,4000,4031,4062,4093,4124,4155,4186,4217,4248,
4279,4310,4341,4372,4403,4434,4465,4496,4527,4558,4589,4620,4651,4682,4713,4744,4775,4806,4837,4868,4899,4930,4961,4992,5023,5054,
5085,5116,5147,5178,5209,5240,5271,5302,5333,5364,5395,5426,5457,5488,5519,5550,5581,5612,5643,5674,5705,5736,5767,5798,5829,5860,
5891,5922,5953,5984,6015,6046,6077,6108,6139,6170,6201,6232,6263,6294,6325,6356,6387,6418,6449,6480,6511,6542,6573,6604,6635,6666,
6697,6728,6759,6790,6821,6852,6883,6914,6945,6976,7007,7038,7069,7100,7131,7162,7193,7224,7255,7286,7317,7348,7379,7410,7441,7472,
7503,7534,7565,7596,7627,7658,7689,7720,7751,7782,7813,7844,7875,7906,7937,7968,7999,8030,8061,8092,8123,8154,8185,8216,8247,8278,
8309,8340,8371,8402,8433,8464,8495,8526,8557,8588,8619,8650,8681,8712,8743,8774,8805,8836,8867,8898,8929,8960,8991,9022,9053,9084,
9115,9146,9177,9208,9239,9270,9301,9332,9363,9394,9425,9456,9487,9518,9549,9580,9611,9642,9673,9704,9735,9766,9797,9828,9859,9890,
9921,9952,9983,10014,10045,10076,10107,10138,10169,10200,10231,10262,10293,10324,10355,10386,10417,10448,10479,10510,10541,10572,
10603,10634,10665,10696,10727,10758,10789,10820,10851,10882,10913,10944,10975,11006,11037,11068,11099,11130,11161,11192,11223,
11254,11285,11316,11347,11378,11409,11440,11471,11502,11533,11564,11595,11626,11657,11688,11719,11750,11781,11812,11843,11874,
11905,11936,11967,11998,12029,12060,12091,12122,12153,12184,12215,12246,12277,12308,12339,12370,12401,12432,12463,12494,12525,
12556,12587,12618,12649,12680,12711,12742,12773,12804,12835,12866,12897,12928,12959,12990,13021,13052,13083,13114,13145,13176,
13207,13238,13269,13300,13331,13362,13393,13424,13455,13486,13517,13548,13579,13610,13641,13672,13703,13734,13765,13796,13827,
13858,13889,13920,13951,13982,14013,14044,14075,14106,14137,14168,14199,14230,14261,14292,14323,14354,14385,14416,14447,14478,
14509,14540,14571,14602,14633,14664,14695,14726,14757,14788,14819,14850,14881,14912,14943,14974,15005,15036,15067,15098,15129,
15160,15191,15222,15253,15284,15315,15346,15377,15408,15439,15470,15501,15532,15563,15594,15625,15656,15687,15718,15749,15780,
15811,15842,15873,15904,15935,15966,15997,16028,16059,16090,16121,16152,16183,16214,16245,16276,16307,16338,16369,16400,16431,
16462,16493,16524,16555,16586,16617,16648,16679,16710,16741,16772,16803,16834,16865,16896,16927,16958,16989,17020,17051,17082,
17113,17144,17175,17206,17237,17268,17299,17330,17361,17392,17423,17454,17485,17516,17547,17578,17609,17640,17671,17702,17733,
17764,17795,17826,17857,17888,17919,17950,17981,18012,18043,18074,18105,18136,18167,18198,18229,18260,18291,18322,18353,18384,
18415,18446,18477,18508,18539,18570,18601,18632,18663,18694,18725,18756,18787,18818,18849,18880,18911,18942,18973,19004,19035,
19066,19097,19128,19159,19190,19221,19252,19283,19314,19345,19376,19407,
19438,19469,19500,19531,19562,19593,19624,19655,19686,19717,19...

As you can see this is a little bit sub-optimal ;) and will be a performance 
issue if the list grows more and more.

Is there a better solution out in the world ?

Regards,
Dirk

-- 


Echte DSL-Flatrate dauerhaft für 0,- Euro*. Nur noch kurze Zeit!
"Feel free" mit GMX DSL: http://www.gmx.net/de/go/dsl

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Reply via email to