如何从Django中的两个模型获取数据

时间:2018-07-16 06:40:38

标签: python django serialization django-models

我一直在尝试使用旧数据库。我已经使用inscpectdb创建了模型文件,但是现在无法在表上执行联接。

我有两个表job_info和username_userid。

这是我的models.class文件:

class UseridUsername(models.Model):
    userid = models.IntegerField(blank=True, null=True)
    username = models.CharField(max_length=100, blank=True, null=True)

    class Meta:
       managed = False
       db_table = 'userid_username'

class LinuxJobTable(models.Model):
    job_db_inx = models.AutoField(primary_key=True)
    mod_time = models.IntegerField()
    account = models.TextField(blank=True, null=True)
    exit_code = models.IntegerField()
    job_name = models.TextField()
    id_job = models.IntegerField()
    id_user = models.IntegerField()
    class Meta:
        managed = False
        db_table = 'linux_job_table'

Heren是我的可序列化的类:

class UseridUsernameSerializer(serializers.ModelSerializer):
    class Meta:
        model = UseridUsername
        fields = ('userid','username')

class UserSerializer(serializers.ModelSerializer):
class Meta:
    username =  UseridUsernameSerializer(many=False)
    model = LinuxJobTable
    fields = ('account','mod_time','username')

0 个答案:

没有答案