我查看了所有相关答案,但没有解决我的问题。我试图保存一个没有运气的对象。我可以看到执行的视图和我为对象更新的值,但更改没有反映在数据库中。以下是视图和模型的代码段。
views.py
class Workspace(_LoggedInMixin, View):
def get(self, request):
user = self.request.user
components = ComponentModel.objects.filter(Q(user=user) | Q(user=None)).order_by('time')
component_status = request.session.get('component', None)
request.session['component'] = None
params = dict(components=components, status=component_status)
return render(request, 'workspace.html', params)
def post(self, request):
data = request.POST
formtype = data['form-type']
error = None
user = self.request.user
if formtype == 'component':
if data['action'] == 'create':
try:
if not os.path.exists('D:/' + self.request.user.username):
os.makedirs('D:/' + self.request.user.username)
cparent = ComponentModel.objects.get(pk=data['cparent'])
component = ComponentModel(user=user, name=data['cname'], time=dt.now(), stats=data['cstats'],
output=data['coutput'], parent=cparent)
component.save()
file = open('D:/' + self.request.user.username + '/' + str(component.id) + '.py', 'w+')
usercode = data['usercode']
usercode = "\n".join(usercode.split("\r\n"))
file.write(usercode)
component.codefile = 'D:/' + self.request.user.username + '/' + str(component.id) + '.py'
component.save()
request.session['component'] = {'name': data['cname'], 'message': 'Component created successfully!'}
except Exception as e:
component.delete()
error = e.message
elif data['action'] == 'delete':
try:
c = ComponentModel.objects.get(pk=data['compid'])
cname = c.name
c.delete()
os.remove('D:/' + self.request.user.username + '/' + data['compid'] + '.py')
request.session['component'] = {'name': cname, 'message': 'Component deleted successfully!'}
except Exception as e:
error = e.message
elif data['action'] == 'save':
try:
if not os.path.exists('D:/' + self.request.user.username):
os.makedirs('D:/' + self.request.user.username)
cparent = ComponentModel.objects.get(pk=data['cparent'])
component = ComponentModel.objects.get(pk=data['compid'])
component.user = user
component.name = data['cname']
component.time = dt.now()
component.stats = data['cstats']
component.output = data['coutput']
component.parent = cparent
component.save()
print component
file = open('D:/' + self.request.user.username + '/' + str(component.id) + '.py', 'w+')
usercode = data['usercode']
usercode = "\n".join(usercode.split("\r\n"))
file.write(usercode)
request.session['component'] = {'name': data['cname'], 'message': 'Component saved successfully!'}
except Exception as e:
error = e.message
if error is not None:
components = ComponentModel.objects.filter(Q(user=user) | Q(user=None)).order_by('time')
params = dict(error=error, components=components)
return render(request, 'workspace.html', params)
return redirect('/workspace')
models.py
class ComponentModel(models.Model):
class Meta:
# overriding the default table name with the following name
db_table = 'components'
verbose_name = 'components'
get_latest_by = 'time'
user = models.ForeignKey('auth.User', on_delete=models.CASCADE, null=True)
name = models.CharField(max_length=255)
time = models.DateTimeField(db_column='created_on')
codefile = models.CharField(max_length=100, db_column='code_file_name', null=True)
stats = models.TextField(db_column='statistical_fields', null=True)
output = models.TextField(db_column='output_fields')
parent = models.ForeignKey('self', on_delete=models.CASCADE, null=True)
def save(self, *args, **kwargs):
try:
super(ComponentModel, self).save(self, *args, **kwargs)
except Exception as e:
return "Something went wrong while saving the component. Details - %s" % e.message
def __str__(self):
return "{id: %s, name: %s, user: %s, time: %s, parent: %s}" % (
self.id, self.name, self.user, self.time, self.parent)
data['action'] == 'create'
中的第二次保存调用和data['action'] == 'save'
中的保存调用未更新数据库。我感谢任何帮助。谢谢。
答案 0 :(得分:2)
您的超级电话不正确。更新以便read ...
dow not %eof;
do stuff
read ...
enddo;
不会被调用为方法参数 - 因为您已经编写它将始终失败。
此外,在未完成保存时,您不会引发异常,因此除非查看std输出,否则您不知道是否存在错误。您可能希望这实际上引发错误。
更新 -
self