vendredi 14 novembre 2014

python - django autoincrement basé sur foreignkey - Stack Overflow


Let's say I have a model Issue that looks like this:


class Issue(models.Model):
title = models.CharField(max_length=100)
author = models.CharField(max_length=100)
text = models.TextField()

And another one called Comment like so:


class Comment(models.Model):
issue = models.ForeignKey(Issue)
text = models.TextField()
number = models.AutoField()

I want the comment number field to be relative to the issue.


Which means that every issue will have its separate comment#1, comment#2, comment#3...etc.


Can this be done in Django?




If you removed the number field from your Comment model and replaced it with a date_added field, you could still query the DB to pull all comments associated with a certain entry in order.


class Comment(models.Model):
issue = models.ForeignKey(Issue)
text = models.TextField()
date_added = models.DateField(auto_add_now = True)

Then if you wanted to get all of the comments associated with a certain issue:


i = Issue.object.get(pk=1)    #pk = primary key id for whatever issue you want

comments_by_issue = i.comment_set.all().order_by('date_added')

Now you have some comments that you can refer to by index location (comments_by_issue[0] would get you the first comment attached to the issue, for instance).


That index location is the closest way I can figure to get what you're looking for. Someone else mentioned comment.id, but this is just going to be an autoincrementing integer that goes up for every comment. The fifth comment added to your system might have comment.id = 5, but it might be the first comment attached to issue 2 - if I'm reading your query right, having the comment ID doesn't help in that context.


This is more of a workaround than a direct answer, but I hope it helps.



Let's say I have a model Issue that looks like this:


class Issue(models.Model):
title = models.CharField(max_length=100)
author = models.CharField(max_length=100)
text = models.TextField()

And another one called Comment like so:


class Comment(models.Model):
issue = models.ForeignKey(Issue)
text = models.TextField()
number = models.AutoField()

I want the comment number field to be relative to the issue.


Which means that every issue will have its separate comment#1, comment#2, comment#3...etc.


Can this be done in Django?



If you removed the number field from your Comment model and replaced it with a date_added field, you could still query the DB to pull all comments associated with a certain entry in order.


class Comment(models.Model):
issue = models.ForeignKey(Issue)
text = models.TextField()
date_added = models.DateField(auto_add_now = True)

Then if you wanted to get all of the comments associated with a certain issue:


i = Issue.object.get(pk=1)    #pk = primary key id for whatever issue you want

comments_by_issue = i.comment_set.all().order_by('date_added')

Now you have some comments that you can refer to by index location (comments_by_issue[0] would get you the first comment attached to the issue, for instance).


That index location is the closest way I can figure to get what you're looking for. Someone else mentioned comment.id, but this is just going to be an autoincrementing integer that goes up for every comment. The fifth comment added to your system might have comment.id = 5, but it might be the first comment attached to issue 2 - if I'm reading your query right, having the comment ID doesn't help in that context.


This is more of a workaround than a direct answer, but I hope it helps.


0 commentaires:

Enregistrer un commentaire