Hello,
I have been searching since the last couple of days and dont see a clear answer, hence posting here. Here is what my application is
1) There are Users ( CustomUser)
2) There are Forms (PDFForm)
3) There are Fields(Field)
4) Each Form has multiple Fields with attributes(PDFFormField)
5) Each User has multiple fields associated with them(UserProfile)
6) Users can request PDFs (GeneratedPDF)
Imagine a User requesting a PDF. A row is created in GeneratedPDF model
This PDF has multiple fields, which need to be populated by values coming from the User Profile Fields (Both UserProfile and PDFFormField has the fields as a FK)
Here is how my Models look
from django.contrib.auth.models import AbstractUser
from django.db import models
from django.conf import settings
class CustomUser(AbstractUser):
pass
# add additional fields in here
#Class has reference to all PDFForms
class PDFForm(models.Model):
#pass
pdf_type=models.IntegerField(default=0)
pdf_name=models.CharField(max_length=100,default='')
file_path=models.FileField(default='')
#Class has reference to every form Field created
class Field(models.Model):
#pass
field_type=models.IntegerField(default=0)
#class relates form field id with PDF ID, a pdf can have multiple fields of same kind.
class PDFFormField(models.Model):
#pass
pdf=models.ForeignKey('PDFForm', on_delete=models.CASCADE,default=0)
field=models.ForeignKey(Field, on_delete=models.CASCADE,default=0)
field_page_number=models.IntegerField(default=0)
field_x=models.DecimalField(max_digits=6,decimal_places=2,default=0)
field_y=models.DecimalField(max_digits=6,decimal_places=2,default=0)
field_x_increment=models.DecimalField(max_digits=6,decimal_places=2,default=0)
class Meta:
ordering= ("field_page_number",)
#class stores extra user data, which is treated as a form field.
class UserProfile(models.Model):
#pass
user=models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE,default=0)
field=models.ForeignKey(Field, on_delete=models.CASCADE,default=0)
field_text=models.CharField(max_length=200,default='')
field_date=models.DateField()
#class has reference to all pdfs users have generated/requested
class GeneratedPDF(models.Model):
user=models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE,default=0)
pdf=models.ForeignKey('PDFForm', on_delete=models.CASCADE,default=0)
date_created=models.DateTimeField(auto_now=True)
class Meta:
ordering= ("date_created",)
A typical query would look like this
select * from PDFFormField A, UserProfile B where B.pdf=<pdf_id> and A.user=<user_id> and A.field=B.field
The above query will match the Fields by ID in both the tables UserProfile and PDFformField for The specific user <user_id> who has request a specific pdf <pdf_id>
I cannot find a documented example which can help me achieve the above query using ORM. I am thinking that I have to resort to using cursors to execute a custom SQL.
Please advise if I should use the cursors route or modify my Models in someway.
Thank you
Rupin