Python Daily
2.57K subscribers
1.48K photos
53 videos
2 files
38.9K links
Daily Python News
Question, Tips and Tricks, Best Practices on Python Programming Language
Find more reddit channels over at @r_channels
Download Telegram
Extending the user model when user is created

I read the docs and a bit of it went over my head when it comes to this. I have a form to create an account that works. I just want it to also create the UserData object to extend the attributes.
class UserData(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
testfield = models.CharField(max_length=100, null=True, blank=True)

That's what is in my models.py, along with importing User at the top.

Then my views.py has:
class UserFormView(View):
form_class = UserForm
template_name = 'main/registration_form.html'

# display blank form
def get(self, request):
form = self.form_class(None)
return render(request, self.template_name, {'form': form})

#process form data
def post(self, request):
form = self.form_class(request.POST)
if form.is_valid():

user = form.save(commit=False)

# cleaned (normalized) data
username = form.cleaned_data['username']
password = form.cleaned_data['password']
user.set_password(password)
user.save()

#returns User objects if credentials are correct
user = authenticate(username=username, password=password)

if user is not None:

if user.is_active:

login(request, user)
return redirect('main:index')

return render(request, self.template_name, {'form': form})

My question is, how can I create the UserData object for that User when it's made? I'm fairly new and feel like I'm by the seat of my pants here, but this is kind of the final piece of the website I'm making, so I'd love to just get it working and then do a deep dive on REALLY understanding what's going on (and any resources for that would be great. The docs confuse me a good bit, but I suppose starting from the top might help).

Thanks

/r/django
http://redd.it/5i68f5
I just made the most 50/50 script ever: it selects and opens random image URLs from 4chan (returns NSFW results like half of the time)

It's fun to run! Cause you *really* never know what's going to pop up. Run at your own risk though, cause it can return anything from cute kitten pictures, to not-unseeable NSFL pictures

#!/usr/bin/python3
#*************************************************************************************************************************
#IMPORTANT
#Don't remove the time.sleeps; which are in place to comply with 4chan's API rule of 'no more than 1 request per second'
#https://github.com/4chan/4chan-API
#
#This script selects a random images from 4chan, and opens them in web browser
#Requires the 'requests' module
#*************************************************************************************************************************

import requests,random,json,time,webbrowser

#Returns [ random image URL, random image's thread URL ]
def r4chan():
#List of 4chan boards
boards = ['a','c','w','m','cgl','cm','n','jp','vp','v','vg','vr','co','g','tv','k','o','an','tg','sp','asp','sci','int','out','toy','biz','i','po','p','ck','ic','wg','mu','fa','3','gd','diy','wsg','s','hc','hm','h','e','u','d','y','t','hr','gif','trv','fit','x','lit','adv','lgbt','mlp','b','r','r9k','pol','soc','s4s']

#Select a board
board = random.choice(boards)

#Request board catalog, and get get a list of threads on the board; then sleeping for 1.5 seconds
threadnums = list()


/r/Python
https://redd.it/ccrh6o