I know already that are some limitations when using Many-to-Many and proxy 
models, I just want to know what is the best practices when doing something 
in this case:
obs: the Choices is from model_utils (Django model utils)

I have this first class:

class Foo(models.Model):
>     "The single table model"
>     TYPE_CHOICES = Choices(
>                            (0,'foo','Foo'),
>                            (1,'foo1','Foo1'),
>                            )
>     type = models.SmallIntegerField(choices=TYPE_CHOICES,default=0)
>     
>     
>     def __unicode__(self):
>         return "%s - %s" %(self.pk,self.type)


Then I have this proxy model

> class Foo1(Foo):
>         
>     objects = TypeAwareManager('type',1)    

    def __cast_from(self,super_instance):
>         #change this instance fields for the super_instance one
>         #but only the ones that matter.        
>         pass

 

    

    def __init__(self, *args, **kwargs):        
>         super(Foo1, self).__init__(*args, **kwargs)
>         #:cast
>         if args != ():
>             if isinstance(args[0],Foo):
>                 self.__cast_from(args[0])
>         self.type = 1


>     class Meta:
>         proxy= True

    
>     def save(self, *args, **kwargs):
>         self.type = 1

        super(Foo1, self).save(*args, **kwargs)


and it's QueryManager:

> class TypeAwareManager(models.Manager):
>     
>     def __init__(self,type_field, type, *args, **kwargs):
>         super(TypeAwareManager, self).__init__(*args, **kwargs)
>         self.type = type
>         self.type_field = type_field
>     
>     def get_query_set(self):
>         return super(TypeAwareManager, 
> self).get_query_set().filter(**{self.type_field:self.type})


Then I have this other model:

> class Bar(models.Model):
>     "a class that has connection to other proxies"
>     
>     
>     def __unicode__(self):
>         return "%s" %(self.pk)

 
And finally, I have the many-to-many model:

class FooBar(models.Model):
>     "many to many foo to bars"
>     
>     foo = models.ForeignKey('my_app.Foo',related_name="%(class)s_list")
>     bar = models.ForeignKey('my_app.Bar',related_name="%(class)s_list")
>     
>     value = models.SmallIntegerField("Value",default=0)
>         
>         
>     def __unicode__(self):
>         return "%s - %s - %s" %(self.pk, self.foo, self.bar)

    
Now the problem:
If I add this field to Bar:

>     foos = 
> models.ManyToManyField('my_app.Foo',related_name='bars_list',through='my_app.FooBar')


Then I can do:

> f = Foo()
> f.save()
>
 

f1 = Foo1()
> f1.save()
> f1 = Foo1()
> f1.save()


> b = Bar()
> b.save()
> b.foobar_list.create(foo=f)
> b.foobar_list.create(foo=f2)


> b.foos.all()
> [<Foo: 1 - 0>, <Foo: 3 - 1>] 


But I wanted Bar to a many-to-many with Foo1, not Foo.
And make it  through Foo1Bar(another proxy model but for Foo1Bar) so that 
it could get in the querys a Foo1 object and not a Foo one when doing this:

> b.foobar_list.get(pk=1).foo
> <Foo1: 1 - 1> instead of <Foo: 1 - 0>


But, when I try to make this many-to-many to Foo1 instead of 
Foo(been through FooBar or Foo1Bar), I get this error:

> Error: One or more models did not validate:
> my_app.bar: 'foos' is a manually-defined m2m relation through model 
> Foo1Bar, which does not have foreign keys to Foo1 and Bar


And this makes sense... so my question is about the best approach in this 
case.


Should I create this many-to-many table as a normal model(no proxy) and set 
the foo foreingkey to Foo1 instead of Foo?
Or should I use only FooBar as many-to-many and create some specific 
queries that would convert ALL the returned foo objects as Foo1 instance?

And if I'm going to have not only Foo1 proxy model, but Foo2, Foo3, and 
Foo4 and maybe Foo5... which approach is the best?


And of course the most important: I need to have this connection from Bar 
to any of this Foo1,2,3,4 as much clear as possible.


Thanks.
If it's going to help this is the git repo that i'm testing this case:
https://github.com/arruda/django_single_table/blob/bf3134865850c8275038d934bd3e1dde91db6b93/my_app/models.py

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/IZiS5y0nQpoJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to