limit choices of records of a ForeignKey field

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

limit choices of records of a ForeignKey field

Benedict Verheyen-3

Hi,

i have a model "Patient" that has a field active. Active = True by
default. If the users want to delete a patient, they actually want to
first deactivate the patient meaning setting the active field to False.

All lists and records that link to this patient model should only
display patients that are active. For instance, there is a model that
deals with the nutritional requirements of the patient so when this form
is displayed, only nutritional requirements should be able to be
registered for active patients. (dropdown list should only show active
users)

So how do i limit the records returned by a ForeignKey?

I use my own Add & Change manipulator based on the standard ones.

2 of the models:
class Patient(models.Model):
    class Admin:
        pass
    opnamenr = models.PositiveIntegerField(blank=False)
    voornaam = models.CharField(maxlength=30, blank=False)
    achternaam = models.CharField(maxlength=30, blank=False)
    adres = models.CharField(maxlength=50, blank=True)
    postcode = models.PositiveSmallIntegerField(blank=True, null=True)
    gemeente = models.CharField(maxlength=30, blank=True)
    geboortedatum = models.DateField(blank=True, null=True)
    arts = models.ForeignKey(Arts, blank=True, null=True)
    kamer = models.ForeignKey(Kamer, blank=True, null=True)
    active = models.BooleanField(blank=False,default=True)
    def __str__(self):
        return self.voornaam + ' ' + self.achternaam

class Mna(models.Model):
    class Admin:
        pass
    patient = models.ForeignKey(Patient, null=False)
    datum = models.DateField(auto_now=True, blank=False)
    eetlust = models.PositiveSmallIntegerField(blank=True, null=True)
    motoriek = models.PositiveSmallIntegerField(blank=True, null=True)
    bmi = models.PositiveSmallIntegerField(blank=True, null=True )
    def __str__(self):
        s = "%s %s " % ( self.datum, str(self.patient) )
        return s

Thanks,
Benedict


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: limit choices of records of a ForeignKey field

Benedict Verheyen-3

Benedict Verheyen schreef:

<snip>

I found a solution in the "limited_choices_to" argument of the
ForeignKey function.
I get the results i want by specifiying this:
patient = models.ForeignKey(Patient, null=False, limit_choices_to =
{'actief':True})
instead of
patient = models.ForeignKey(Patient, null=False )

For my lists, i use a view so i can do this to get there:
   #p = Patient.objects.all().order_by('-opnamenr')
   p = Patient.objects.filter(actief=True).order_by('-opnamenr')

So it all works.
But i'm interested in other ways of getting this kind of filtering.

Is it possible to do this by making a custom filter or a custom
templatetag ?
I haven't made a custom filter of template tag yet or didn't find the
need for one until i encountered this problem. It looks like a good
candidate for a tag/filter.

Regards,
Benedict


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---