Hello,

I am trying to develop sublcass of models.ManyToManyField. This class
should return text representations of objects to forms.TextField (like
TagField from django-tagging). The reason to make my own
representation is because:
- I don't need content types
- I need "front face" to ManyToManyField, but TagField uses CharField
and stores duplicate data.

So I come to implementing my own descriptor and field like this
(minimal code just to show exception, dpaste of same code for syntax
highlighting http://dpaste.com/60470/):

#descriptor of field
class MultiModelSubclassDescriptor
(ReverseManyRelatedObjectsDescriptor):

    def __init__(self, m2m_field):
        super(MultiModelSubclassDescriptor, self).__init__(m2m_field)

    def __get__(self, instance, instance_type=None):
        from products.models import Ingridient
        return ", ".join(["%s" % ing.name for ing in
Ingridient.objects.all()])
        #super(MultiModelSubclassDescriptor, self).__get__(instance,
instance_type)

    def __set__(self, instance, value):
        pass
        #super(MultiModelSubclassDescriptor, self).__set__(instance,
value)

#field
class MultiModelSubclassField(ManyToManyField):

    def __init__(self, to, **kwargs):
        super(MultiModelSubclassField, self).__init__(to, **kwargs)

    def contribute_to_class(self, cls, name):
        super(MultiModelSubclassField, self).contribute_to_class(cls,
name)

        # Make this object the descriptor for field access.
        setattr(cls, self.name, MultiModelSubclassDescriptor(self))

    def value_from_object(self, obj):
        "Returns the value of this field in the given model instance."
        return getattr(obj, self.attname) #original getattr(obj,
self.attname).all() for queryset

    def formfield(self, **kwargs):
        f = FormCharField(widget = TextInput())
        return f





But as my field is a subclass of ManyToManyField, django tries to get
PKs of my object, not value.
Piece of django code which is responsible for it:

    for f in opts.fields + opts.many_to_many:
        if not f.editable:
            continue
        if fields and not f.name in fields:
            continue
        if exclude and f.name in exclude:
            continue
        if isinstance(f, ManyToManyField):
            # If the object doesn't have a primry key yet, just use an
empty
            # list for its m2m fields. Calling f.value_from_object
will raise
            # an exception.
            if instance.pk is None:
                data[f.name] = []
            else:
                # MultipleChoiceWidget needs a list of pks, not object
instances.
                data[f.name] = [obj.pk for obj in f.value_from_object
(instance)] """ HERE I get exception """

        else:
            data[f.name] = f.value_from_object(instance)



There is a hack I to overcome this I beleive...

Any help much appreciated, I am fighting with it for 4 days...
Thanks.
--~--~---------~--~----~------------~-------~--~----~
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 
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