Re: A modeling/implementation quiz
On Apr 19, 3:29 pm, Juanjo Conti <[EMAIL PROTECTED]> wrote: > Hi all, > > I am worried about how to model the next scene (this is an example, but > an appropriated one): In the model we have People, and there are > different kind of people, let's say: Professors, Students and Other. > Each People object has a 'type' attribute. Type can be P, S or O. > > So far, all right. > > Each people has an identifier that looks like: {LETTER}{NUMBER}. P1, P2, > P3 and S1 are examples of valid identifiers. > > When you instance a People object you know its type but not its number: > > p = People(type="S") > > The question is, how can I handle this numbers in my model? The count > should be dependent of the type. > > I am thinking about creating PostgreSQL sequences and use them to > retrieve the propitiated number each time a People is instanced: > > ... > if type == "S": > self.number = seq_wrapper_s.next() > elif typ == "P": > self.number = seq_wrapper_p.next() > else: > self.number = seq_wrapper_o.next() > ... > > But this will tie me to an specific data base engine. Is there a way of > doing this task in Django in a independent-db-engine way? You could add an IntegerField and a person_id property that's derived dynamically to give you the P1, S2, etc. identifiers. Then, override the save() method to manage the numeric sequence of those types. class Person(models.Model): numeric_id = models.IntegerField() person_type = models.CharField() # model with options or ForeignKey class Meta: unique_together = ((numeric_id, person_type),) def _person_id(self): return self.person_type + self.numeric_id person_id = property(_person_id) def save(self): if not numeric_id: # Need to assign the next numeric_id for this person_type try: last_of_this_type = Person.objects.filter(person_type=self.person_type).order_by('- numeric_id')[:1].get() next_id = last.numeric_id + 1 except Person.DoesNotExist: next_id = 1 self.numeric_id = next_id super(Person, self).save() As an unrelated side note: for the sake of following good convention, consider using singular words instead of plural ones for your model class names (i.e. Person instead of People.) In your example, I assume that S1 means a single student and P2 means one professor. Then, it makes sense to name your model Person instead of People. -Rajesh D --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: A modeling/implementation quiz
Kenneth Gonsalves escribió: > > On 21-Apr-08, at 8:15 PM, Michael wrote: > >> What about using the count() method on a filtered subset to get your >> id? >> >> Eg: >> p = People(type="S") >> p.id = People.objects.filter(type=p.type).count() + 1 > > I dont know what the context of this is, but this would only give the > id if you have never deleted a People object > And is not multiuser... Juanjo -- mi blog: http://www.juanjoconti.com.ar --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: A modeling/implementation quiz
On 21-Apr-08, at 8:15 PM, Michael wrote: > What about using the count() method on a filtered subset to get your > id? > > Eg: > p = People(type="S") > p.id = People.objects.filter(type=p.type).count() + 1 I dont know what the context of this is, but this would only give the id if you have never deleted a People object -- regards kg http://lawgon.livejournal.com http://nrcfosshelpline.in/code/ --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: A modeling/implementation quiz
What about using the count() method on a filtered subset to get your id? Eg: p = People(type="S") p.id = People.objects.filter(type=p.type).count() + 1 Michael --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: A modeling/implementation quiz
andy baxter escribió: > Is there a strong reason why the count should depend on the type? Yes, I am creating a system for a real state business where this code schema is used for the different type of houses. Juanjo -- mi blog: http://www.juanjoconti.com.ar --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: A modeling/implementation quiz
Juanjo Conti wrote: > Hi all, > > I am worried about how to model the next scene (this is an example, but > an appropriated one): In the model we have People, and there are > different kind of people, let's say: Professors, Students and Other. > Each People object has a 'type' attribute. Type can be P, S or O. > > So far, all right. > > Each people has an identifier that looks like: {LETTER}{NUMBER}. P1, P2, > P3 and S1 are examples of valid identifiers. > > When you instance a People object you know its type but not its number: > > p = People(type="S") > > The question is, how can I handle this numbers in my model? The count > should be dependent of the type. > > I am thinking about creating PostgreSQL sequences and use them to > retrieve the propitiated number each time a People is instanced: > > ... > if type == "S": > self.number = seq_wrapper_s.next() > elif typ == "P": > self.number = seq_wrapper_p.next() > else: > self.number = seq_wrapper_o.next() > ... > > But this will tie me to an specific data base engine. Is there a way of > doing this task in Django in a independent-db-engine way? > > Is there a strong reason why the count should depend on the type? Can't you have a more standard model with two fields - 'id' (which would be a normal auto-increment primary key field) and 'type' (which could be 'P', 'S', or 'O', or else you could model it as a SmallIntegerField, with a set of valid choices)? e.g. PERSON_TYPE_CHOICES=((1,'Professor'),(2,'Student'),(3,'Other')) class Person(models.Model) # (no need to put in the id field explicitly as it will be generated by django.) type=models.SmallIntegerField(choices=PERSON_TYPE_CHOICES) ... any other person fields. --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
A modeling/implementation quiz
Hi all, I am worried about how to model the next scene (this is an example, but an appropriated one): In the model we have People, and there are different kind of people, let's say: Professors, Students and Other. Each People object has a 'type' attribute. Type can be P, S or O. So far, all right. Each people has an identifier that looks like: {LETTER}{NUMBER}. P1, P2, P3 and S1 are examples of valid identifiers. When you instance a People object you know its type but not its number: p = People(type="S") The question is, how can I handle this numbers in my model? The count should be dependent of the type. I am thinking about creating PostgreSQL sequences and use them to retrieve the propitiated number each time a People is instanced: ... if type == "S": self.number = seq_wrapper_s.next() elif typ == "P": self.number = seq_wrapper_p.next() else: self.number = seq_wrapper_o.next() ... But this will tie me to an specific data base engine. Is there a way of doing this task in Django in a independent-db-engine way? Thanks in advance, Juanjo -- mi blog: http://www.juanjoconti.com.ar --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---