[英]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.