Today I had a good dream in which I though I fixed something in my bug-magnet. This was the outcome:
# The Error
# The Error
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/main
Django Version: 1.4.1
Python Version: 2.7.3
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'main',
'django.contrib.admin')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/home/rob/b4buy/main/views.py" in main
23. form.save()
File "/home/rob/b4buy/main/forms.py" in save
9. super(self.__class__, self).save(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/forms/models.py" in save
364. fail_message, commit, construct=False)
File "/usr/local/lib/python2.7/dist-packages/django/forms/models.py" in save_instance
86. instance.save()
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py" in save
463. self.save_base(using=using, force_insert=force_insert, force_update=force_update)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py" in save_base
551. result = manager._insert([self], fields=fields, return_id=update_pk, using=using, raw=raw)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/manager.py" in _insert
203. return insert_query(self.model, objs, fields, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py" in insert_query
1576. return query.get_compiler(using=using).execute_sql(return_id)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.py" in execute_sql
910. cursor.execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/util.py" in execute
40. return self.cursor.execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/base.py" in execute
337. return Database.Cursor.execute(self, query, params)
Exception Type: IntegrityError at /main
Exception Value: main_country.ruler_id may not be NULL
# the views.py
from django.shortcuts import render_to_response
from django.core.context_processors import csrf
from django.http import HttpResponseRedirect
from main.models import *
from django.forms.models import modelform_factory
from main.forms import CountryForm
def main(request):
c = {}
c.update(csrf(request))
c['title'] = 'Main'
c['NO_COUNTRY'] = False
form = CountryForm()
c['form'] = form
if not request.user.is_authenticated():
return HttpResponseRedirect('/')
if not request.user.country_set.all():
c['NO_COUNTRY'] = True
if request.method == 'POST':
form = CountryForm(request.POST)
form.ruler = request.user
if form.is_valid():
form.save()
return render_to_response("main.html", c)
# the models.py
from django.db import models
from web.models import User
class Space(models.Model):
name = models.CharField(max_length=50)
owner = models.ForeignKey("Object")
def __unicode__(self):
return u'{} of {}'.format(self.name, self.owner)
class Object(models.Model):
name = models.CharField(max_length=100)
container = models.ForeignKey(Space, blank=True, null=True)
def __unicode__(self):
return self.name + u' object'
class Country(Space):
type = models.CharField(choices=(('1', 'farmer'), ('2', 'producer'),
('3', 'traveler')), max_length=1,
default='1')
position = models.CommaSeparatedIntegerField(max_length=2)
ruler = models.ForeignKey(User)
class Material(Object):
mass = models.IntegerField()
quality = models.IntegerField()
class Meta:
abstract = True
def use():
self.delete()
class Tool(Object):
mass = models.IntegerField()
class Meta:
abstract = True
class Hammer(Tool):
def slam(self, object):
object.times_slammed += 1
object.save()
class Room(Space):
max_items = models.IntegerField()
def add(self, obect):
object.container = self
object.save()
class Building(Object):
usable = models.BooleanField(default=False)
def build(self):
for object in container.object_set.all():
if object in self.needed_materials:
self.needed_materials.remove(object)
object.use()
if not needed_materials:
self.usable = True
# the forms.py of course
from django import forms
from django.forms import HiddenInput
from main.models import Country, Object
class CountryForm(forms.ModelForm):
def save(self, *args, **kwargs):
self.position = '1, 1'
self.owner = Object.objects.filter(name='Universe')[0]
super(self.__class__, self).save(*args, **kwargs)
class Meta:
fields = ('name', 'type')
model = Country
I add the Object Universe manually via the admin
If anybody could share a little help, I would gladly receive it.
Drvanon
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/Ye1OKiEEDEsJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
No comments:
Post a Comment