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 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
reddit
Extending the user model when user is created • /r/django
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...
Simple python script that mutes sound when Spotify app runs an ad
Hey guys, was a bit distracted by the fact that Spotify Free is killing the mood sometimes in a foreign language, so decided to create a script that mutes all the sound whenever there is an ad playing.
This script only works on Windows.
This script get windll libraries and uses them to create a process name list (mostly copied code).
After the list is built, it is checked for Process names "Advertisement" and "Spotify" to see if an ad is playing. These names are specific to the moment when ad is being played in Spotify.
The script is run in an interval, and does not fetch data real-time, so has small delays in runtime. As it is short and easily processed, does not load up CPU and doesn't leak memory.
The code: (Requires ctypes and pycaw libraries)
import ctypes #process find
import time #sleep
from pycaw.pycaw import AudioUtilities #mute
while True:
EnumWindows = ctypes.windll.user32.EnumWindows
EnumWindowsProc = ctypes.WINFUNCTYPE(ctypes.c_bool, ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int))
/r/Python
https://redd.it/cefijn
Hey guys, was a bit distracted by the fact that Spotify Free is killing the mood sometimes in a foreign language, so decided to create a script that mutes all the sound whenever there is an ad playing.
This script only works on Windows.
This script get windll libraries and uses them to create a process name list (mostly copied code).
After the list is built, it is checked for Process names "Advertisement" and "Spotify" to see if an ad is playing. These names are specific to the moment when ad is being played in Spotify.
The script is run in an interval, and does not fetch data real-time, so has small delays in runtime. As it is short and easily processed, does not load up CPU and doesn't leak memory.
The code: (Requires ctypes and pycaw libraries)
import ctypes #process find
import time #sleep
from pycaw.pycaw import AudioUtilities #mute
while True:
EnumWindows = ctypes.windll.user32.EnumWindows
EnumWindowsProc = ctypes.WINFUNCTYPE(ctypes.c_bool, ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int))
/r/Python
https://redd.it/cefijn
reddit
r/Python - Simple python script that mutes sound when Spotify app runs an ad
139 votes and 35 comments so far on Reddit