简体   繁体   English

django-在查询集中的字段中添加模型中不存在的字段

[英]django - prepending a field in the query set that doesn't exist in the model

I have a model that reads from a pre-existing university database. 我有一个模型,该模型从一个现有的大学数据库中读取。 The username is stored as the students id. 用户名存储为学生ID。 I'm not able to add a new column to the database in a save or I could use a save function in my model. 我无法通过保存将新列添加到数据库中,或者可以在模型中使用保存功能。

class Student(models.Model):
        student_id = models.IntegerField(db_column = 'Student_ID', primary_key = True)
        statusid = models.IntegerField(db_column = 'StatusID')
        fname = models.CharField(db_column = 'Student_First_Name', max_length = 35)
        lname = models.CharField(db_column = 'Student_Last_Name_Formatted' , max_length = 40)



        class Meta:
            managed = False
            db_table = '[Student]'

What i'm trying to do is take the username in a query and match it up with another field that is umich.edu/student_id as a primary key. 我想做的是在查询中获取用户名,并将其与另一个字段umich.edu/student_id匹配为主键。

I'm defining the request in one function as: 我在一个函数中将请求定义为:

def user_list(request):
    student = Student.objects.all()

passing in the argument for students to my template with a POST to the form. 通过POST将表单的POST参数传递给学生的​​模板。

In my next view that gets posted 在我的下一个视图中

def criteria(request):
    user = request.POST.get('student_id')
    print(user)
    student = CourseCriteria.objects.get(universitystudentid = request.POST.get('student_id'))

My print/post of user is coming through correctly as the student id, but I need to prepend umich.edu/ to the student_id in the queryset. 我的用户打印/帖子正确作为学生ID,但是我需要在umset.edu/之前添加到queryset中的student_id。 How can I accomplish this without being able to add that field to my model? 如何在不将该字段添加到模型中的情况下完成此操作?

I am not sure to understand your problem, I suppose that it is not as simple as: 我不确定您的问题,我想这不是那么简单:

def criteria(request):
    user = request.POST.get('student_id')
    student = CourseCriteria.objects.get(universitystudentid='umich.edu/%s'%user)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Django model 字段与其他不存在的 model 字段冲突? - Django model field clashes with other model field that doesn't exist? Django 不存在 - 匹配查询不存在 - Django DoesNotExist - Matching Query doesn't exist Django中不存在匹配查询 - Matching query doesn't exist in django 在创建具有相关模型中不存在的多对多字段选择的 Django 模型对象时,如何防止表单提交? - How to keep a form from submitting when creating a Django model object with many-to-many field selection that is doesn't exist in the related model? 在不存在的模型字段上获取迁移错误 - Getting Migrate Error on Model Field that doesn't exist Django:将列添加到模型中导致该列不存在的错误 - Django: Adding column to model giving error that the column doesn't exist 现有表的Django动态模型“不存在” - Django dynamic model for existing table “doesn't exist” Django extra_views - UpdateWithInlinesView:字段不存在 - Django extra_views - UpdateWithInlinesView: field doesn't exist Django消息/通知(如果连接的模型不存在/已更改) - Django messages/notification if connected model doesn't exist / has changed Django-关系数据库模型中的POST方法-表不存在 - Django - POST method in relational database model - table doesn't exist
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM