Monday, February 28, 2011

Re: Model Question

I'm not sure about your first question; I've never tried overriding a field in a subclass.

For your second question are you trying to check on the whole query set, or an individual model instance?

If you're checking on an individual model instance, you can use hasattr() like:

if hasattr(instance, 'field_name'):
# do stuff

Otherwise, the way you specified it is pretty much the only way I know how to do it. I made it a method on my model that looks like the following:

def get_field_names(self):
return [field.name for field in ModelName._meta.fields]

You would call it by instantiating a 'blank' instance of the model:

fields = ModelName().get_field_names()

On Feb 25, 2011, at 12:11 PM, Noah Nordrum wrote:

> I'm trying to cram the ORM into an existing schema and have an issue I
> can't seem to get around.
>
> I have a number of tables with a timestamp column, but the column name
> is inconsistent. I would like to put the timestamp field in an
> abstract superclass, but I can't seem to figure out how to override
> the column name in the subclass. Can I do this?
>
> Also, is there a better way to check for existence of a field in a
> models.Manager method than the following:
>
> def filteredResults(self):
> qs = super(MyManager, self).get_query_set()
> for field in qs.model._meta.fields:
>
> It works, but not sure how hacky this is...
>
> New to django and python (from primarily Java recently).
>
> --
> 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+unsubscribe@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
>

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

No comments:

Post a Comment