سؤال

I have a django app (w/postgresql database) that stores information on nest conditions for an endangered bird. Data is collected over multiple sites with different #'s of nests at each site. The nest conditions also have a unique date range per site.

DB Columns: site_name, date, nest_01, nest_02, nest_03 ... all the way to nest_1350. The nests have values of either empty, 1E, 2E, 3E, or 4E.

Is there a way to do 1 query of all (1-1350) of the nest columns looking for '1E'?

Thanks

هل كانت مفيدة؟

المحلول

Do you actually have a model with 1350+ columns? If I were you I'd normalize the whole setup like this:

class Site(Model):
    site_name = Charfield()
    date = DateField()

class Nest(Model):
    name = Charfield()
    condition = Charfield()
    site = ForeignKey(Site)

And then query it like this:

site = Site.objects.get(pk=1) # just a Site, I assume you know a Site
nests = Nest.objects.filter(site=site).filter(condition='1E') # your desired nests
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top