kindly correct me with the tittle if it is wrong or misleading, thanks.
I am very new to Django Python. I am using Django 3.1.1.
My question is about the "Model".objects.create()
, I use it with ajax to save or create data in my Logger
model.
Question: How can I link the Logger.userprofile
to UserProfile.user
using Logger.objects.create()
or what is the next thing to do to linked the two fields?
My UserProfile
model is already linked to the User
model via OneToOneField
but was saving data using form.save()
, and this was successful. below is what I have constructed so far by following numerous tutorials.
This is already working and it is creating data in my Model, I just need to add the link between the UserProfile
and the Logger
for the sake of easy identification of the log created.
Log is created in the Logger
Model via qrcode scan.
Views.py
def processQRCode(request):
if request.method == 'POST':
qrcoderes = request.POST['qrcoderes']
logged_in = request.POST['logged_in']
logged_in_location = request.POST['logged_in_location']
Logger.objects.create(
qrcode_uuid = qrcoderes, #edited from qrcode_uuid to qrcoderes to distinguish the two variables.
logged_in = logged_in,
logged_in_location = logged_in_location
)
return HttpResponse('Success!')
Models.py
UserProfile Model
class UserProfile(models.Model):
GENDER = (
('Male', 'Male'),
('Female', 'Female'),
)
user = models.OneToOneField(User, related_name='userprofile', null=True, on_delete=models.SET_NULL)
qrcode_uuid = models.UUIDField(unique=True, default=uuid4)
qr_code = models.ImageField(upload_to='qr_codes', blank=True)
firstName = models.CharField(max_length=200)
middleName = models.CharField(max_length=200)
lastName = models.CharField(max_length=200)
gender = models.CharField(max_length=6, choices=GENDER)
birthdate = models.DateField(max_length=200)
contact = models.CharField(max_length=200)
agency = models.CharField(max_length=200)
deptSec = models.CharField(max_length=200)
designation = models.CharField(max_length=200)
streetHouse = models.CharField(max_length=200)
purok = models.CharField(max_length=200)
barangay = models.CharField(max_length=200)
cityMunicipality = models.CharField(max_length=200)
province = models.CharField(max_length=200)
date_created = models.DateTimeField(auto_now_add=True)
class Meta:
verbose_name_plural = 'User Profile'
def __str__(self):
return self.firstName + " " + self.middleName + " " + self.lastName
def save(self, *args, **kwargs):
qrcode_img = qrcode.make(self.qrcode_uuid)
canvas = Image.new('RGB', (370, 370), 'white')
draw = ImageDraw.Draw(canvas)
canvas.paste(qrcode_img)
fileName = f'qr-code-{self.qrcode_uuid}.png'
buffer = BytesIO()
canvas.save(buffer,'PNG')
self.qr_code.save(fileName, File(buffer), save=False)
canvas.close()
super().save(*args, **kwargs)
Logger Model
class Logger(models.Model):
userprofile = models.OneToOneField(UserProfile, null=True, on_delete=models.SET_NULL)
qrcode_uuid = models.CharField(max_length=200, null=True)
logged_in = models.CharField(max_length=1, default="1")
logged_in_location = models.CharField(max_length=200, null=True)
log_datetime = models.DateTimeField(auto_now_add=True)
class Meta:
verbose_name_plural = 'Logger'
def __str__(self):
return self.logged_in_location + " " + self.qrcode_uuid
Ajax.js
function processQRCodeDetails(){
$.ajax({
type:'POST',
url: '/processqrcode/',
data:{
qrcoderes: $('#process-qrcoderes').val(),
logged_in: $('#logged_in').val(),
logged_in_location: $('#logged_in_location').val(),
csrfmiddlewaretoken: $('input[name="csrfmiddlewaretoken"]').val()
},
success: function(data){
$('#staticBackdrop').modal();
console.log('Success');
},
error: function(data){
console.log('Error')
}
});
}
template.html
<form id="process-qrcoderes-form">
{% csrf_token %}
<input class="form-control" type="text" name="process-qrcoderes" id="process-qrcoderes" onchange="processQRCodeDetails(); return false;" autofocus="autofocus">
<input type="text" name="logged_in" id="logged_in" value="1">
<input type="text" name="logged_in_location" id="logged_in_location" value="test">
</form>