In django, the Data Query condition is that the table columns corresponding to the foreign key in the table should be searched ?, Django table
1. Generally, the condition for finding data in a table is the existing data type in python. You can use the dot operator to directly find the data. If the condition is a column in the table corresponding to a foreign key column, how do I query data? For example
class News(models.Model): title = models.CharField(max_length=50); summary = models.TextField(); url = models.CharField(max_length=150); favorCount = models.IntegerField(default=0); favorUsername = models.TextField(default=""); replyCount = models.IntegerField(default=0);class Reply(models.Model): content = models.TextField(); user = models.ForeignKey('User'); newID = models.ForeignKey('News'); replyTime = models.DateTimeField(auto_now_add=True); def __unicode__(self): return self.content;
For a data table like this, do you want to find all replies for blog id 3?
Method 1: first obtain the objects in the table pointed to by the foreign key, and then use the '_ set' method to obtain the data in the target table.
obj = models.News.objects.get(id=3)replys = obj.reply_set.all()
Method 2: directly use double underscores (_) in the target table to specify the fields in the table corresponding to the foreign key to find the objects that meet the conditions.
models.Reply.objects.filter(newID__id=3)
This can be used in the filter, or in the values method. The value is the data in the table corresponding to the foreign key.
2. You cannot avoid data interaction with the front-end when using django, but the data types in python are rich. For example, the datetime type of the datetime module cannot be json encoded, so you need a stronger django. serializers. serialize method. Generally, the json format is serializers. serialize ('json', data ). You can easily encode all data types in django in json format.