Add a "split" field to a model causes problems

Visto 210 veces
Saltar al primer mensaje no leído

Andrew Ingram

no leída,
23 oct 2012, 6:52:1023/10/12
a django-d...@googlegroups.com
Hi all,

This one stung me today. Basically as part of an event (calendar) app, I have functionality for splitting a series of events into two at a given timestamp. The details aren't particularly relevant, but the key thing is that I had a method called "split" on one of the models.

This was working fine until I tried to add a ManyToManyField to the same model, at which point it through a rather ugly error during initialisation of the apps.

django/db/models/fields/related.py line 56, in add_lazy_relation
    app_label, model_name = relation.split(".")
TypeError: unbound method split() must be called with RecurringEvent instance as first argument (got str instance instead)

Because I had no knowledge of the inner workings here, this error wasn't helpful until pdb told me that `relation` was an instance of my model. The issue is that in this point in the code, relation can either be a model or a string 'app_label.model_name', so it's using the presence of a split function to determine which it is, me having a split function on my model breaks this check. Having a non-callable 'split' causes a similar problem, just with a different exception.

Long story short, adding a 'split'  method to my model caused the inner-workings of Django to break, I think the documentation should provide a list of attribute names that can't be added to a Django model without breaking things, though this is the first time I've ever come across such a problem so it might be a one-off.

Regards,
Andrew Ingram

Russell Keith-Magee

no leída,
23 oct 2012, 20:11:0323/10/12
a django-d...@googlegroups.com
Hi Andrew,

Thanks for that gnarly bug report. I'm a little surprised that the 'split' name causes a problem here; based on my knowledge of Django's internals, I wouldn't have thought that the situation you describe shouldn't be a problem. My gut reaction tells me that the actual problem is somewhere else in your model definition, causing the wrong object instance to be passed into the 'relation' variable; the 'split' method is a symptom, rather than the actual problem. However, I may be wrong.

If you can open a bug report with a complete working (or rather, broken) example of the problem, we can dig into it more. If you're right that the split method is the problem, we should certainly document it; if the problem lies somewhere else, there's possibly some better validation that we need to do.

Yours,
Russ Magee %-)

Alex Gaynor

no leída,
23 oct 2012, 20:13:1823/10/12
a django-d...@googlegroups.com

--
You received this message because you are subscribed to the Google Groups "Django developers" group.
To post to this group, send email to django-d...@googlegroups.com.
To unsubscribe from this group, send email to django-develop...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-developers?hl=en.

No this is a bug in Django, I see it.


Basically some genius (very likely me), decided we shouldn't type check for string or model, and just try to call split.

It's an easy enough fix. Please file a bug and if there's no patch I'll do it tonight.

Alex

--
"I disapprove of what you say, but I will defend to the death your right to say it." -- Evelyn Beatrice Hall (summarizing Voltaire)
"The people's good is the highest law." -- Cicero

Stephen Burrows

no leída,
3 nov 2012, 17:38:303/11/12
a django-d...@googlegroups.com
I've opened a ticket to track this issue.

Andrew Ingram

no leída,
4 nov 2012, 16:10:094/11/12
a django-d...@googlegroups.com
Hi Mike,

It looks like I missed part of the problem, namely that abstract models are involved. These model definitions should trigger the error:

from django.db import models


class Bar(models.Model):
name = models.CharField(max_length=250)


class AbstractFoo(models.Model):
name = models.CharField(max_length=250)
bars = models.ManyToManyField(Bar)

def split(self):
pass

class Meta:
abstract = True


class Foo(AbstractFoo):
pass

It doesn't make a difference if the ManyToManyField is on the abstract model or the concrete one.

Regards,
Andrew


On 4 Nov 2012, at 20:42, Mike Johnson <mrjoh...@gmail.com> wrote:

Hi Andrew,

I'm trying to come up with a test case for this and am struggling to reproduce the problem in test.

Can you post some code to help us reproduce it?

Thanks,
Mike



On Tuesday, October 23, 2012 3:52:10 AM UTC-7, Andrew Ingram wrote:
--
You received this message because you are subscribed to the Google Groups "Django developers" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-developers/-/95Ok89lqNjkJ.

Mike Johnson

no leída,
4 nov 2012, 16:17:184/11/12
a django-d...@googlegroups.com
Ahh, got it now. Thanks!
Responder a todos
Responder al autor
Reenviar
0 mensajes nuevos