this is my code for my project, I just get this error I tried to figure it out but I don't get it,
Django Error:
DoesNotExist at /save_post/
Profile matching query does not exist.
views.py, line 75, in save_post
form.authore = Profile.objects.get(user=request.user)
views.py
#login_required
def save_post(request):
if request.method == "POST":
form = Post(content=request.POST['content'])
form.authore = Profile.objects.get(user=request.user)
form.save()
elif request.method == "PUT":
data = json.loads(request.body)
post_id = int(data["post_id"])
new_content = data["new_content"]
post = Post.objects.filter(id=post_id).first()
if post.authore.user != request.user:
return HttpResponse(status=401)
post.content = new_content
post.save()
return JsonResponse({
"result": True
}, status=200)
else:
return JsonResponse({
"error": "post not found"
}, status=400)
return index(request)
models.py
class User(AbstractUser):
pass
class Profile(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
class Post(models.Model):
content = models.TextField()
timestamp = models.DateTimeField(default=timezone.now)
authore = models.ForeignKey(Profile, on_delete=models.CASCADE)
likes = models.PositiveIntegerField(default=0, blank=True, null=True)
def serialize(self):
return {
"id": self.id,
"content": self.content,
"timestamp": self.timestamp.strftime("%b %#d %Y, %#I:%M %p"),
"authore": self.authore.id,
"username": self.authore.user.username,
"likes": self.likes.count(),
}
this seems correct but you already this user you are using doesn't have profile
so here my notes :
You can use get_object_or_404 this if profile not found will return not found
from django.shortcuts import get_object_or_404
form.authore = get_object_or_404(Profile, user=request.user)
when user-created no logic here profile must created so it is logical user doesn't have profile
it is better to use one_to_one relation ship when creating user profile as each user has only one profile and vice versa
to make it correct working refer here
https://simpleisbetterthancomplex.com/tutorial/2016/07/22/how-to-extend-django-user-model.html
go to Extending User Model Using a One-To-One Link in previous link
field execute this tutorial and user profile will created auto when user created
Related
Whenever I try to create a "Tour" for a "User" I get this error:
"DoesNotExist at /add-tour/FAjK5CryF8/ - User matching query does not exist."
Specifically the problems seems to come from this line of code:
user = User.objects.get(pk=pk)
models.py
class Tour(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
tour_date = models.DateField(default=date.today)
tour_fans = models.ForeignKey(FanAccount, on_delete=models.PROTECT)
def __str__(self):
return f"{self.user} del {self.tour_date}"
views.py
def tour_new(request, pk):
user = User.objects.get(pk=pk)
if request.method == "POST":
form = TourForm(request.POST)
if form.is_valid():
instance = form.save(commit=False)
instance.tour_fans = request.user
form.instance.user = user
instance.save()
form.save()
return render(request, "tour.html", {"form": TourForm(), "success": True})
else:
form = TourForm()
return render(request, "tour.html", {"form": form})
For "User" I'm using a custom Primary Key (ShortUUID).
I'm new to Python and Django so it may be something easily solvable, but after hours of attempts I seem unable to solve it.
You need to make sure your user instance does exist, so you should instead of user = User.objects.get(pk=pk)call user = get_object_or_404(User, pk=pk), then you need to pass user as an instance:
if request.method == "POST":
form = TourForm(request.POST, instance=user)
I am working on a Django-Tenant (Multi-Tenant) application. I am writing the script to create the subdomain. I am trying to get it to where created_by is set to the current users id that is logged in. How can I get current user ID to populate the created_by field?
views.py
class CreatePortal(View):
def get(self, request):
form = CreatePortalForm()
return render(request, "registration/create_portal.html", {"form": form})
def post(self, request):
form = CreatePortalForm(request.POST)
if form.is_valid():
getDomain = form.cleaned_data.get('name')
instance = form.save(commit=False)
tenant = Client(schema_name=getDomain, name=getDomain, created_by=**[NEED USER ID HERE]**)
tenant.save()
domain = Domain()
domain.domain = (getDomain + ".example.com:8000")
domain.tenant = tenant
domain.is_primary
domain.save()
with schema_context(tenant.schema_name):
instance.save()
redirect = 'http://' + getDomain + '.example.com:8000'
return HttpResponseRedirect(redirect)
return render(request, "registraton/create_portal.html", {"form": form})
forms.py
class CreatePortalForm(forms.ModelForm):
class Meta:
model = Client
fields = ["name"]
models.py
This is the line that I am working with models.py
created_by = models.ForeignKey(User, on_delete=models.CASCADE)
I also get an error if it is not User type and if I do not pass an actual number for id, Field 'id' expected a number but got datetime.datetime
I have tried this as well:
user = request.user.id
tenant = Client(schema_name=getDomain, name=getDomain, created_by=user)
but then get this error:
Cannot assign "1": "Client.created_by" must be a "User" instance.
I am not sure how to pass the current logged in Users ID to populate the created_by form field. Any and all help is appreciated. I am still learning Django.
This is a problem which I've been fighting with for a while and now I gave up. I am writing a User/Profile model in Django 2.0.2 (python 3.6 + postgres 10 on Linux) which is as follows :
class Config_Table(models.Model):
entity_name = models.TextField(primary_key=True)
category = models.TextField()
description = models.TextField(blank=True,default='')
Above table keeps some static information which gets me into trouble.
class UserProfile(AbstractUser):
"The Profile of a user with details are stored in this model."
username = models.TextField(primary_key=True, max_length=11)
first_name = models.TextField(max_length=50,blank=True,default='')
last_name = models.TextField(max_length=100,blank=True,default='')
phone_number = models.TextField(max_length=11,blank=True,default='')
avatar = models.ImageField(blank=True, upload_to='Pictures/')
GENDER_CHOICES = (
('M','Male'),
('F','Female'),
)
gender = models.CharField(max_length=1,choices=GENDER_CHOICES, default='M')
city = models.TextField(max_length=25, blank=True, default='NY')
description = models.TextField(max_length=2000, blank=True, default='')
interests = models.ManyToManyField(Config_Table, blank=True, default='')
date_of_birth = models.DateField(blank=True)
official_docs = models.ImageField(blank=True, upload_to='Pictures/')
team_name = models.TextField(blank=True,default='')
debit_card_number = models.IntegerField(blank=True, default=0)
MUSIC_CHOICES = (
('Rock','Rock Music'),
('Trad','Traditional Music'),
('Elec','Electronic Music'),
('Clas','Classical Music')
)
favorite_music = ArrayField(models.TextField(blank=True,default=''),size=2,blank=True, default='{}')
class Meta:
permissions=(("User","User level permission"),
("Tour","Tourleader level permission"),
("Admin","Administrators"))
my views.py :
class UserList(APIView):
"""
List all users, or create a new user.
"""
def get(self, request, format=None):
users = UserProfile.objects.all()
target_users = []
for user in users.iterator():
if user.is_superuser == False:
target_users.append(user)
serializer = UserProfileSerializer(target_users, many=True)
return Response(serializer.data)
def post(self, request, format=None):
serializer = UserProfileSerializer(data=request.data)
if serializer.is_valid(raise_exception=True):
serializer.save()
print(serializer.errors)
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
class UserDetail(APIView):
"""
Retrieve, update or delete a User.
"""
pk_url_kwarg = 'username'
def get_object(self, pk):
try:
return UserProfile.objects.get(pk=pk)
except UserProfile.DoesNotExist:
raise Http404
def get(self, request, pk, format=None):
user = self.get_object(pk=pk)
if user.is_superuser == False:
serializer = UserProfileSerializer(user)
return Response(serializer.data)
else:
return Response(status=status.HTTP_404_NOT_FOUND)
def put(self, request, pk, format=None):
user = self.get_object(pk)
serializer = UserProfileSerializer(user, data=request.data, partial=True)
if serializer.is_valid():
for attr, value in serializer.validated_data.items():
if attr == 'password' and attr is None:
serializer.validated_data['password'] = user.password
break
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def delete(self, request, pk, format=None):
user= self.get_object(pk)
user.delete()
return Response(status=status.HTTP_204_NO_CONTENT)
and my serializers.py:
def create(self, validated_data):
hashed_password = make_password(validated_data['password']) # get the hashed password
print(validated_data)
user = UserProfile(
username=validated_data['username'],
email = validated_data['email'],
first_name= validated_data['first_name'],
last_name= validated_data['last_name'],
phone_number=validated_data['phone_number'],
avatar=validated_data.pop('avatar'),
gender=validated_data['gender'],
city=validated_data['city'],
description=validated_data['description'],
date_of_birth=validated_data.pop('date_of_birth'),
# user_type=validated_data['user_type'],
official_docs=validated_data.pop('official_docs'),
team_name=validated_data['team_name'],
debit_card_number=validated_data['debit_card_number'],
favorite_music=validated_data['favorite_music'],
)
user.set_password(hashed_password)
interest = validated_data['interests']
user.interests.add(validated_data['interests'])
user.groups.add(validated_data['groups'])
user.save()
return user
The problem I have happens inside the serializer, when I want to submit some JSON object through http, like below (sample data for testing) :
{
"username": "12345678004",
"password": "thisisatest",
"last_login": null,
"is_superuser": false,
"email" : "sample#gmail.com",
"first_name": "AAA",
"last_name": "BBB",
"phone_number": "12045678000",
"gender": "M",
"city": "NY",
"description": "",
"date_of_birth": "2010-03-28",
"team_name": "",
"avatar": "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxAPBhAREQ8QDQ8PDxIWEBAWFRANDxAQFRUWFhURExUYHSggGBolGxUTITEhJSkrLi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tVo4rjPKrv38gpRtdtzTXiYxLUAAAAAAAAAAAAAAAAAACIzOO670emi3b/unnP2Q9l2M1zVPKOXlaAAAAAAAAA4azTxct+scpUlVMxVieEw9ErNq2MVRVHXn57grwAAAAAAAAAAAAAAAAbW4zciO8x9QXekt7unpj0diOQAAAAAAAAA5aq3vWJj04eXUB5yRtejF2fMtQAAAAAAAAAAAAAAG9n8an/KPq0InE57A9GMUTmiJ7wyAAAAAAAAADFdWKJntAKDUT/Oq8tGa5zXPlgAAAAAAAAAAAAAAAAFxs29vWMdafolqLR39y9E9J4T4XkTmMxxiQZAAAAAAAARNpXd2xjrV9OqVM4jM8IhSay/v3pnpHLwDgAAAAAAAAAAAAAAAAAAnaDWbvs1cuk9kEB6OJ4Cp0F65E4iJqp7f9W0cgAAAAAVevuXJ4TE00+nHPkDX6ze9mn3es90AAAAAAAAAAAAAAAAAAAddPYqrrxH5z0gHOiiaqsRGZWWm2dEca+Pp0S9Pp6aKeEces9ZdQYpiIjEcIZAAAAAAnkAIep0FNWZp9mfkq71mqirEx+kvQNLtqKqcTGQefEjV6Sbc96Z5T9kcAAAAAAAAAAAAAGaKZmqIjnIN9PZmu5iPznsu7FmKLeI/dppNPFu1jrPOXcAAAAAAAAAAAAGK6YmnExmJUut0v8OvvTPKfsu2l63FduYnlIPPje/ami5MT+8NAAAAAAAAAAAFlsvT/wBc/wCv6oFm3vXYiOsr6imKaIiOUA2AAAAAAAAAAAAAAABE2jp9+1mPep+cKd6NSa6zuX57TxgEcAAAAAAAAAFjsm1xmr8o+6ycdJb3dPTHpxdgAAAAAAAAAAAAAAAAEPadresZ60/RMa105omO8A88NrlOLkx2lqAAAAAAA6aejev0x3lzStmU51XiJkFyAAAAAAAAAAAAAAAAAAACl2jRjUz6oyw2vT7VM+ivAAAAAAATdk/jz4AFsAAAAAAAAAAAAAAAAAAACBtb8KPKrAAAAAH/2Q==",
"debit_card_number": 0,
"favorite_music": [],
"groups": [1],
"user_permissions": [],
"interests": ["Ski"]
}
It always returns TypeError:
user.interests.add(validated_data['interests'])
Exception Type: TypeError at /users/
Exception Value: unhashable type: 'list'
I have tried different ways of implementing "interests" and "groups" in the serializer create function. I tried parsing validated data, selecting the child from Config_table and adding it here, but none of them work.
I actually have the same problem with "groups" field as well. It seems that Django cannot unhash the validated data in serializer and there it throws error.
The interesting point is, if I don't fill in the "group" and "interests" field when calling the POST method, it works fine, and later, I can update those fields by calling PUT with no problem.
What can I do with this?
You should pass to interests.add method different interests objects. Not list of names. So first you need get objects by name and then pass unpacking list of objects to add method using * syntax:
intersts = []
for name in validated_data['interests']:
obj, created = Config_Table.objects.get_or_create(entity_name=name)
interests.append(obj)
user.interests.add(*interests)
OK, I resolved the issue like below :
I changed Config_table model and added default django id field (technically, just removed primary_key from entity_name row, so django added the id AutoField by default). Re-populated the database and changed the code in serializers.py like below..using the answer of #neverwalkaloner :
user = UserProfile(
...
)
user.set_password(hashed_password)
user.save()
interests = []
for id in validated_data['interests']:
interests.append(id)
user.interests.add(*interests)
for name in validated_data['groups']:
user.groups.add(name)
user.save()
return user
Note that I had to save the entity before I wanted to work with a ManyToManyField. As django expects to see something in the database prior to adding some entities for ManyToMany fields. Keep that note in your code.
With this workaround, my problem has been resolved. But it is still strange to me why I could not use a primary key rather than the default. If anyone has an answer to this question, I'm more than willing to hear.
I am trying to have a logged in user add Media to their media field in the users profile in a DRF generic create view. Has anyone tried this? Here's my view and model:
class MediaCreate(generics.CreateAPIView):
"""
To create a media object, send a post request to:
/profiles/media/create/
In the format:
Audio: "audio file upload"
Title: "char field"
"""
queryset = Media.objects.all()
serializer_class = MediaSerializer
class Musician(ProfileModel):
summary = models.TextField(blank=True)
company = models.CharField(max_length=60, blank=True)
media = models.ManyToManyField('Media', blank=True)
timestamp = models.DateTimeField(auto_now_add=True, blank=True)
def __str__(self):
return '{}'.format(self.user.username)
So I decided to do this with a function api view instead so I can add the model to the profile using the request and not requiring any get request
I know the check if method == Post is redundant but it doesn't harm anything:
#api_view(['POST'])
def MediaCreate(request):
context = {}
logged_on = False
if request.user.is_authenticated():
logged_on = True
visitor = request.user.musician
serializer = MediaSerializer(data=request.data)
if request.method == "POST":
if serializer.is_valid():
serializer.save()
try:
x = serializer.instance
visitor.media.add(x)
context['upload'] = True
except:
error = "Media Not Added to Profile"
context['logged_on',
'error',
'upload'] = logged_on, error, False
return JsonResponse(
data=context,
status=status.HTTP_400_BAD_REQUEST)
context['logged_on'] = logged_on
return JsonResponse(data=context, status=status.HTTP_200_OK)
all you need to do define in your serializers.py the two classes:
class MediaSerializer(serializers.ModelSerializer):
class Meta:
model = Media
class MusicianSerializer(serializers.ModelSerializer):
media = MediaSerializer(many=True, read_only=true)
class Meta:
model = Musician
In order to update the media field for specific musician you send only the media_id and not the whole object as show the following example:
data = {
"summary": "text summary here"
"company": "Company A"
"media_id": 3
}
in your views.py you should define the following view and override get_object to fit your needs:
class MusicianUpdatingApiView(generics.UpdateAPIView):
"""
To add a media to a user, send a post request to:
/profiles/:id/media
"""
serializer_class = MusicianSerializer
def get_object(self):
summary = self.request.data.get("summary")
company = self.request.data.get("company")
media_id = self.request.data.get("media_id")
musician_id = self.kwargs['id'] # cause it send in the url
data = {
"summary": summary
"company": company
"media_id": media_id
}
updated_musician, created = Musician.objects.update_or_create(
id=musician_id,
defaults=data
)
return updated_musician
in urls.py
url(r'^profiles/(?P<id>\d+)/media$', MusicianUpdatingApiView.as_view()),
I've been struggling with this all week long, and I need to put it to rest once and for all. This might look like a lot of code, but at the core is a simple conceptual question.
I have a model, UserProfile, that has the following fields:
user = models.OneToOneField(User)
weekOne = models.OneToOneField(WeekOne)
weekTwo = models.OneToOneField(WeekTwo)
WeekOne and WeekTwo are both models with their own unique fields (models.Model) that inherit from a custom class called Week. Week just has a few custom functions to save some re-typing of methods for each week and the following code to make it an abstract class:
class Meta:
abstract = True
Basically, I want every user to have a unique weekOne and weekTwo (and beyond) field that has custom fields with values that are unique to the user.
When I first create a user (i.e., when they sign up), I use the following code in views.py:
def signup(request):
user_form = UserCreateForm(data=request.POST)
if request.method == 'POST':
if user_form.is_valid():
username = user_form.clean_username()
password = user_form.clean_password2()
user_form.save()
user = authenticate(username=username, password=password)
login(request, user)
return redirect('/')
else:
return index(request, user_form=user_form)
return redirect('/')
Basic form signup stuff, everything has always worked fine here.
Now, here's where things get dicey. I have a view for weekOne that makes sure a user's profile has been created and creates one if not, the code for which is as follows:
#login_required
def workout1(request):
template = "workout1.html"
weekOne = WeekOne()
weekOne.save()
user, created = UserProfile.objects.get_or_create(user=request.user,
defaults = {'weekOne': weekOne})
name = weekOne.__unicode__()
if created:
context = {'user': user}
return render(request, template, context)
# Grab already existing User Profile
weekOne.delete() # Was never used
context = {'user': user, 'name': name}
return render(request, template, context)
Okay. So that's cool. But when I try to go to the page for week one, I get the following error:
workout_game_app_userprofile.weekTwo_id may not be NULL
This is where I'm lost. Should I be initializing every single week variable for every single week view? I.e., for the week one view, should I be doing code like this:
weekOne = WeekOne()
weekTwo = WeekTwo()
weekThree = WeekThree()
etc.? This seems absurdly repetitive if I have to do it for all 12 weeks I'm planning on implementing.
Btw, my models were functioning perfectly well before I implemented the second week.
Also, is OneToOne the right kind of key to use? I want to do things like access user.weekOne.item1, user.weekOne.item2, etc. and change and save their values only for that user.
UPDATE: For Sidharth Shah, here's the rest of the code from my views and models:
views.py:
from django.shortcuts import render, redirect
from django.contrib.auth import login, authenticate, logout
from django.contrib.auth.models import User
from workout_game_app.forms import AuthenticateForm, UserCreateForm
from workout_game_app.models import WeekOne, WeekTwo, UserProfile
from django.http import Http404, HttpResponse
from django.contrib.auth.decorators import login_required
from django.views.decorators.csrf import csrf_exempt
from django.core import serializers
import simplejson
def index(request, auth_form=None, user_form=None):
if request.user.is_authenticated():
user = request.user
name = "Missions Overview"
context = {'user': user, 'name': name}
template = 'workouts.html'
return render(request, template, context)
else:
auth_form = auth_form or AuthenticateForm()
user_form = user_form or UserCreateForm()
template = 'index.html'
context = {'auth_form': auth_form, 'user_form': user_form}
return render(request, template, context)
def login_view(request):
if request.method == 'POST':
form = AuthenticateForm(data=request.POST)
if form.is_valid():
login(request, form.get_user())
return redirect('/')
else:
return index(request, auth_form=form)
return redirect('/')
def signup(request):
user_form = UserCreateForm(data=request.POST)
if request.method == 'POST':
if user_form.is_valid():
username = user_form.clean_username()
password = user_form.clean_password2()
user_form.save()
user = authenticate(username=username, password=password)
login(request, user)
return redirect('/')
else:
return index(request, user_form=user_form)
return redirect('/')
#login_required
def submitWorkout1(request):
if request.method == 'POST':
exercise = request.POST['exercise']
try:
amount = request.POST['amount']
except KeyError: # No amount field on form
amount = ""
user = UserProfile.objects.get(user=request.user)
week = user.weekOne
exercise, amount, exerciseComplete, allComplete = user.updateExercise(week, exercise, amount)
data = simplejson.dumps({
'result': 'success',
'exercise': exercise,
'amount': amount,
'exerciseComplete': exerciseComplete,
'allComplete': allComplete
}, indent=4)
return HttpResponse(data)
#login_required
def workout2(request):
template = "workout2.html"
weekTwo = WeekTwo()
weekTwo.save()
user, created = UserProfile.objects.get_or_create(user=request.user,
defaults = {'weekTwo': weekTwo})
name = weekTwo.__unicode__()
if created:
context = {'user': user}
return render(request, template, context)
# Grab already existing User Profile
weekTwo.delete() # Was never used
context = {'user': user, 'name': name}
return render(request, template, context)
#login_required
def submitWorkout2(request):
if request.method == 'POST':
exercise = request.POST['exercise']
try:
amount = request.POST['amount']
except KeyError: # No amount field on form
amount = ""
user = UserProfile.objects.get(user=request.user)
week = user.weekTwo
exercise, amount, exerciseComplete, allComplete = user.updateExercise(week, exercise, amount)
data = simplejson.dumps({
'result': 'success',
'exercise': exercise,
'amount': amount,
'exerciseComplete': exerciseComplete,
'allComplete': allComplete
}, indent=4)
return HttpResponse(data)
and models.py:
from django.db import models
from django.contrib.auth.models import User
class Week(models.Model):
# List of exercises by name for the week
exercises = []
# Week name in unicode
name = u''
# Running count of benchmarks met.
completeCount = models.PositiveSmallIntegerField(default=0)
# Set to true if benchmarks reached.
weekComplete = models.BooleanField(default=False)
# A bunch of methods
class WeekOne(Week):
name = u'Mission One'
exercises = ['squats', 'lunges', 'stairDaysCount', 'skipStairs']
# Required benchmarks for given exercises
squatBenchmark = 1000
lungeBenchmark = 250
stairDaysCountBenchmark = 3
totalGoals = 4
squats = models.PositiveIntegerField(default=0)
lunges = models.PositiveIntegerField(default=0)
skipStairs = models.BooleanField(default=False)
stairDaysCount = models.PositiveSmallIntegerField(default=0)
# A bunch of methods
class WeekTwo(Week):
name = u'Mission Two'
exercises = ['up3Levels', 'noHands', 'treadmill', 'vagMachine', 'extendedStairs']
totalGoals = 5
up3Levels = models.BooleanField(default=False)
noHands = models.BooleanField(default=False)
treadmill = models.BooleanField(default=False)
vagMachine = models.BooleanField(default=False)
extendedStairs = models.BooleanField(default=False)
# A bunch of methods
class UserProfile(models.Model):
user = models.OneToOneField(User)
weekOne = models.OneToOneField(WeekOne, null=True, default=None)
weekTwo = models.OneToOneField(WeekTwo, null=True, default=None)
# Some methods
and, though it works fine, my forms.py for good measure:
from django.contrib.auth.forms import AuthenticationForm, UserCreationForm
from django.contrib.auth.models import User
from django import forms
from django.utils.html import strip_tags
class UserCreateForm(UserCreationForm):
username = forms.CharField(required=True,
widget = forms.widgets.TextInput(attrs={'placeholder': 'Username'}))
password1 = forms.CharField(required=True,
widget = forms.widgets.PasswordInput(attrs={'placeholder': 'Password'}))
password2 = forms.CharField(required=True,
widget = forms.widgets.PasswordInput(attrs={'placeholder': 'Password'}))
def is_valid(self):
form = super(UserCreateForm, self).is_valid()
for f, error in self.errors.iteritems():
if f != '__all_':
self.fields[f].widget.attrs.update({'class':'error', 'value':strip_tags(error)})
return form
class Meta:
fields = ['username', 'password1', 'password2']
model = User
class AuthenticateForm(AuthenticationForm):
username = forms.CharField(
widget = forms.widgets.TextInput(attrs={'placeholder':'Username'}))
password2 = forms.CharField(
widget = forms.widgets.PasswordInput(attrs={'placeholder': 'Password'}))
You might want to have following model
user = models.OneToOneField(User)
weekOne = models.OneToOneField(WeekOne, null=True, default=None)
weekTwo = models.OneToOneField(WeekTwo, null=True, default=None)
Try that out, it should work. Looking at the code above you're defining fields weekOne, weekTwo etc. What I am not sure of is if you're assigning all necessary fields weekOne object while creating it.