Sunday, April 22, 2012

Re: Modeling Question on Many-to-Many(with through) and Proxy models

So using your tip, I've done this:

class Foo1Bar(FooBar):
    
    objects = TypeAwareManager('foo__type',1)
    
    class Meta:
        proxy= True
        
def change_fb(cls):
    old_foo = cls.foo
    
    @property
    def new_foo(self):
        return Foo1(self.old_foo) if self.old_foo != None else None
    
    setattr(cls, 'old_foo', old_foo)
    setattr(cls, 'foo', new_foo)
    
    return cls

This way when calling a Foo1Bar the foo attr would be the cast from Foo to Foo1 if it exist.
And then in Bar I've done what you said:

class Bar(models.Model):
    "a class that has connection to other proxies"
    foos = models.ManyToManyField('my_app.Foo',related_name='bars_list',through='my_app.FooBar')
    
    @property
    def foo1bar_list(self):
        qr = self.foobar_list.filter(foo__type=1)
        qr.model = Foo1Bar
        return qr
    
    def __unicode__(self):
        return "%s" %(self.pk)

And this is aparently working, but I still want to make some more tests.

Em domingo, 22 de abril de 2012 14h10min33s UTC-3, Arruda escreveu:
Thanks, I saw that .model thing too, but was also unsure about it... I might just test it.

Em sábado, 21 de abril de 2012 19h04min30s UTC-3, akaariai escreveu:
> 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?

I am not sure if I am answering your question... But here is a dirty
hack to try:
qs = SomeModel.objects.all()
qs.model = SomeModelProxy
print list(qs)

You should get proxy model instances from the queryset.

Now, I just quickly tested this a minute ago, and it seems to work in
a very simple cases. The above uses internals of Django's ORM in a way
it was not designed to be used. So mandatory warnings: If anything
breaks, though luck. A minor version upgrade could break your code
without any notice.

So, in the m2m case I guess I would create a property:
class SomeModel:
     foos = M2M(Foo)

     def _foo1s(self):
           qs = self.foos.all()
           qs.model = Foo1
           return qs
     foo1s = property(_foo1s)

The above might just work. Or not. Once again, be very cautious if you
use any of the above.

 - Anssi

Em sábado, 21 de abril de 2012 19h04min30s UTC-3, akaariai escreveu:
On Apr 21, 8:35 pm, Arruda <felipe.arruda.pon...@gmail.com> wrote:
> 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?

I am not sure if I am answering your question... But here is a dirty
hack to try:
qs = SomeModel.objects.all()
qs.model = SomeModelProxy
print list(qs)

You should get proxy model instances from the queryset.

Now, I just quickly tested this a minute ago, and it seems to work in
a very simple cases. The above uses internals of Django's ORM in a way
it was not designed to be used. So mandatory warnings: If anything
breaks, though luck. A minor version upgrade could break your code
without any notice.

So, in the m2m case I guess I would create a property:
class SomeModel:
     foos = M2M(Foo)

     def _foo1s(self):
           qs = self.foos.all()
           qs.model = Foo1
           return qs
     foo1s = property(_foo1s)

The above might just work. Or not. Once again, be very cautious if you
use any of the above.

 - Anssi

Em sábado, 21 de abril de 2012 19h04min30s UTC-3, akaariai escreveu:
On Apr 21, 8:35 pm, Arruda <felipe.arruda.pon...@gmail.com> wrote:
> 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?

I am not sure if I am answering your question... But here is a dirty
hack to try:
qs = SomeModel.objects.all()
qs.model = SomeModelProxy
print list(qs)

You should get proxy model instances from the queryset.

Now, I just quickly tested this a minute ago, and it seems to work in
a very simple cases. The above uses internals of Django's ORM in a way
it was not designed to be used. So mandatory warnings: If anything
breaks, though luck. A minor version upgrade could break your code
without any notice.

So, in the m2m case I guess I would create a property:
class SomeModel:
     foos = M2M(Foo)

     def _foo1s(self):
           qs = self.foos.all()
           qs.model = Foo1
           return qs
     foo1s = property(_foo1s)

The above might just work. Or not. Once again, be very cautious if you
use any of the above.

 - Anssi

Em sábado, 21 de abril de 2012 19h04min30s UTC-3, akaariai escreveu:
On Apr 21, 8:35 pm, Arruda <felipe.arruda.pon...@gmail.com> wrote:
> 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?

I am not sure if I am answering your question... But here is a dirty
hack to try:
qs = SomeModel.objects.all()
qs.model = SomeModelProxy
print list(qs)

You should get proxy model instances from the queryset.

Now, I just quickly tested this a minute ago, and it seems to work in
a very simple cases. The above uses internals of Django's ORM in a way
it was not designed to be used. So mandatory warnings: If anything
breaks, though luck. A minor version upgrade could break your code
without any notice.

So, in the m2m case I guess I would create a property:
class SomeModel:
     foos = M2M(Foo)

     def _foo1s(self):
           qs = self.foos.all()
           qs.model = Foo1
           return qs
     foo1s = property(_foo1s)

The above might just work. Or not. Once again, be very cautious if you
use any of the above.

 - Anssi

Em sábado, 21 de abril de 2012 19h04min30s UTC-3, akaariai escreveu:
On Apr 21, 8:35 pm, Arruda <felipe.arruda.pon...@gmail.com> wrote:
> 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?

I am not sure if I am answering your question... But here is a dirty
hack to try:
qs = SomeModel.objects.all()
qs.model = SomeModelProxy
print list(qs)

You should get proxy model instances from the queryset.

Now, I just quickly tested this a minute ago, and it seems to work in
a very simple cases. The above uses internals of Django's ORM in a way
it was not designed to be used. So mandatory warnings: If anything
breaks, though luck. A minor version upgrade could break your code
without any notice.

So, in the m2m case I guess I would create a property:
class SomeModel:
     foos = M2M(Foo)

     def _foo1s(self):
           qs = self.foos.all()
           qs.model = Foo1
           return qs
     foo1s = property(_foo1s)

The above might just work. Or not. Once again, be very cautious if you
use any of the above.

 - Anssi

--
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/-/t-TAznisPNMJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment