How Serializing many-to-many through Foreign Key Django? - django

I have model "B" with many-to-many through Foreign Key:
class DManager(m.Manager):
def get_by_natural_key(self, name):
return self.get(name=name)
class D(m.Model):
objects = DManager()
id = m.AutoField(primary_key=True)
name = m.CharField(max_length=250, unique=True, null=False)
def natural_key(self):
return (self.name)
class A(m.Model):
id = m.IntegerField(unique=True, null=False, primary_key=True)
name = m.CharField(max_length=250, null=True)
class B(m.Model):
id = m.IntegerField(unique=True, null=False, primary_key=True)
name = m.CharField(max_length=250, null=True)
type = m.ForeignKey(D)
bs = m.ManyToManyField(A, through='C')
def natural_key(self):
## ?natural key for many-to-many?
return(self.name, self.type.natural_key(), ?????)
class C(m.Model):
a_id = m.ForeignKey(A)
b_id = m.ForeignKey(B)
I can get relation through foreign key (B-D), but I can't get relation from many-to-many (B-A) in my ajax.py:
....
if request.is_ajax():
aj_d = json.loads(request.body.decode('utf-8'))
raw_data = serializers.serialize(
'python', m.B.objects.filter(
bs__a_id__in=aj_d['data']).distinct(),
use_natural_foreign_keys=True)
output = json.dumps(raw_data)
return HttpResponse(output, content_type='application/json')
Maybe exist another way through values() for example. But I have problem with dumps list of dicts - "is not JSON serializable":
...
raw_data = m.B.objects.filter(
bs__a_id__in=aj_d['data']).distinct().values()
output = json.dumps(raw_data)

Solution:
def push_data(request):
q = m.B.objects
if request.is_ajax():
data = json.loads(request.body.decode('utf-8'))
if 'req_1' in data:
q = q.filter(bs__id__in=data['req_1'])
if 'req_2' in data:
q = q.filter(type__id__in=data['req_2'])
actual_data = q.values('name', 'id', 'type__name')
mtm_get(actual_data) ## down
return HttpResponse(json.dumps(list(actual_data)),
content_type='application/json; charset=utf8')
for many-to-many:
def mtm_get(data):
for d in data:
d['a_name'] = ', '.join(''.join(i) for i in m.B.objects.filter(
pk=d['id']).values_list('bs__name'))

Related

Bulk create on related models using csv

I'm trying to use bulk_create in order to add objects to related models. Here i'm fetching the csv file through post request which contains required fields. As of now I can add items to models which is unrelated using the csv file and bulk_create and it's working.
class BulkAPI(APIView):
def post(self, request):
paramFile = io.TextIOWrapper(request.FILES['requirementfile'].file)
dict1 = csv.DictReader(paramFile)
list_of_dict = list(dict1)
objs = [
ManpowerRequirement(
project=row['project'],
position=row['position'],
quantity=row['quantity'],
project_location=row['project_location'],
requested_date=row['requested_date'],
required_date=row['required_date'],
employment_type=row['employment_type'],
duration=row['duration'],
visa_type=row['visa_type'],
remarks=row['remarks'],
)
for row in list_of_dict
]
try:
msg = ManpowerRequirement.objects.bulk_create(objs)
returnmsg = {"status_code": 200}
print('imported successfully')
except Exception as e:
print('Error While Importing Data: ', e)
returnmsg = {"status_code": 500}
return JsonResponse(returnmsg)
My models are:
class ManpowerRequirement(models.Model):
project = models.CharField(max_length=60)
position = models.CharField(max_length=60)
quantity = models.IntegerField()
project_location = models.CharField(max_length=60)
requested_date = models.DateField()
required_date = models.DateField()
employment_type = models.CharField(max_length=60,choices = EMPLOYMENT_TYPE_CHOICES,
default = 'Permanent')
duration = models.CharField(max_length=60)
visa_type = models.CharField(max_length=60)
remarks = models.TextField(blank = True , null=True)
def __str__(self):
return self.project
class Meta:
verbose_name_plural = "Manpower_Requirement"
class Fulfillment(models.Model):
candidate_name = models.CharField(max_length=60)
manpower_requirement = models.ForeignKey(ManpowerRequirement, on_delete=models.CASCADE)
passport_number = models.CharField(blank = True, max_length=60)
subcontract_vendors = models.CharField(max_length=200,blank = True , null=True ,default='')
joined_date = models.DateField(blank = True, null = True, default = '')
remarks = models.TextField( blank = True,null = True)
def __str__(self):
return self.candidate_name
class Meta:
verbose_name_plural = "Fulfillment"
class FulfillmentStatus(models.Model):
fulfillment = models.ForeignKey(Fulfillment, on_delete=models.CASCADE)
status = models.CharField(max_length=60)
status_date = models.DateField()
remarks = models.TextField( blank = True, null = True )
def __str__(self):
return self.fulfillment.candidate_name
class Meta:
verbose_name_plural = "FulfillmentStatus"
I don't know how to do the same using bulk_create for Fulfillment and FulfillmentStatus models which are related to ManpowerRequirement. Csv file which I recieve in order to bulkcreate for Fulfillment consists of all the fields of ManpowerRequirement and all fields of Fulfillment and FulfillmentStatus excluding the foreign keys and id fields.
in the past I had the same problem; I solved in that way
Assuming that in a single CSV row you have data for all models I'd go for
create a mapping between the main model and the linked ones (you could use row index as key
use bulk_create() on the main model
iterate the dict and use bulk_create() for the linked modules
items = []
mrs = []
for row in list_of_dict:
mr = ManpowerRequirement(...)
mrs.append(mr)
f = ManpowerRequirement(...)
fs = FulfillmentStatus(...)
items.append((mr, f, fs))
# create all Manpower Requirements
ManpowerRequirements.objects.bulk_create(mrs)
a = []
for mr, f, fs in items:
f.manpower_requirement = mr
a.append(f)
# create all Fulfillments
Fulfillment.objects.bulk_create(a)
a = []
for mr, f, fs in items:
fs.fulfillment = f
a.append(fs)
# create all FulfillmentStatus
FulfillmentStatus.objects.bulk_create(a)
not sure if you can do some optimization in looping but it should solve the problem with just 3 queries
For related models we can do like this
class FulfillmentAPI(APIView):
def post(self, request):
paramFile = io.TextIOWrapper(request.FILES['fulfillmentfile'].file)
dict1 = csv.DictReader(paramFile)
list_of_dict = list(dict1)
objs = [
Fulfillment(
manpower_requirement=ManpowerRequirement.objects.get(project=row['project'], position=row['position'], quantity=row['quantity'], requested_date=row['requested_date'],),
remarks=row['remarks'],
candidate_name=row['candidate_name'],
passport_number=row['passport_number'],
joined_date=row['joined_date'],
subcontract_vendors=row['subcontract_vendors'],
)
for row in list_of_dict
]
try:
msg = Fulfillment.objects.bulk_create(objs)
returnmsg = {"status_code": 200}
print('imported successfully')
except Exception as e:
print('Error While Importing Data: ', e)
returnmsg = {"status_code": 500}
return JsonResponse(returnmsg)

How can I obtain just one JSON object with the three models information combined?

How can I pass the foreign key values from my model to my serialised json object?
Now I have this three models,
class Fleet(models.Model):
fleet_id = models.IntegerField('Id flota', primary_key=True, unique=True)
fleet_name = models.CharField('Nombre flota', max_length=20, unique=True)
def __str__(self):
return self.fleet_name + ' ' + str(self.fleet_id)
class Device(models.Model):
dev_eui = models.CharField(max_length=16, primary_key=True, unique=True)
producer = models.CharField(max_length=20)
model = models.CharField(max_length=20)
dev_name = models.CharField(max_length=20, unique=True)
fleet_id = models.ForeignKey(Fleet, on_delete=models.CASCADE)
def __str__(self):
return self.dev_eui
class DevData(models.Model):
data_uuid = models.UUIDField(primary_key=True, default=uuid.uuid1, editable=False)
frequency = models.IntegerField()
data_1 = models.FloatField()
data_2 = models.FloatField(null=True, blank=True)
dev_eui = models.ForeignKey(Device, on_delete=models.CASCADE) #hay que saber porque aƱade _id
def __str__(self):
return self.dev_eui
And what I'm doing is call my view function in my JS code to obtain some data like this.
def getData(request):
ctx = {}
if request.method == 'POST':
select = int(request.POST['Select'])
data = DevData.objects.order_by('dev_eui','-data_timestamp').distinct('dev_eui')
nodes = Device.objects.all()
fleets = Fleet.objects.all()
data = loads(serializers.serialize('json', data))
nodes = loads(serializers.serialize('json', nodes))
fleets = loads(serializers.serialize('json', fleets))
ctx = {'Data':data, 'Nodes':nodes, 'Fleets':fleets}
return JsonResponse(ctx)
And inside my js file I filter it with some if else conditionals.
This works well, but I'm sure I can do it directly in my view but I don't know how. How can I obtain just one JSON object with the three models information combined?
Thank you very much!!
You can write a custom serializer like this:
from django.core.serializers.json import Serializer
class CustomSerializer(Serializer):
def end_object(self, obj):
for field in self.selected_fields:
if field == 'pk':
continue
elif field in self._current.keys():
continue
else:
try:
if '__' in field:
fields = field.split('__')
value = obj
for f in fields:
value = getattr(value, f)
if value != obj and isinstance(value, JSON_ALLOWED_OBJECTS) or value == None:
self._current[field] = value
except AttributeError:
pass
super(CustomSerializer, self).end_object(obj)
Then use it like this
serializers = CustomSerializer()
queryset = DevData.objects.all()
data = serializers.serialize(queryset, fields=('data_uuid', 'dev_eui__dev_eui', 'dev_eui__fleet_id__fleet_name'))
I have wrote an article regarding serializing nested data here. You can check that out as well.

How to get id from foreign key model in django

How Do I get id of to_user from the below model:
class Friend(models.Model):
status = models.CharField(max_length=10)
from_user = models.ForeignKey(AUTH_USER_MODEL, on_delete=models.CASCADE, related_name = 'from_user')
to_user = models.ForeignKey(AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="to_user")
date_modified = models.DateTimeField(auto_now=True, blank=True)
date_created = models.DateTimeField(auto_now_add=True, null=True)
def create(self,request, **kwargs, ):
friend = self.create(from_user_id=request.user.id, status="Pending")
return friend
class Meta:
unique_together = (('from_user', 'to_user'),)
def __str__(self):
return self.to_user.email
my view :
def accept_friend_request(request, uidb64, status):
"""Accept button will lead to entry in database as accepted and reject button will lead to entry in database as rejected based on status flag"""
Friend.status = "pending"
try:
uid = urlsafe_base64_decode(uidb64)
friend_user = User.objects.filter(id=Friend.to_user.id)
f = Friend.objects.filter(friend_id = friend_user)
if f:
f.status=status
f.save()
f.status = "accepted"
return render(request, 'users/friend_list.html', {"uidb64": uid, "status": status})
else:
f.status = "rejected"
f.save()
return render(request, 'users/friend_list.html', {'uidb64':uid, 'status':status})
except AttributeError:
return render(request, 'blog/base.html')
I cannot retrieve the friend_user = User.objects.filter(id=Friend.to_user.id)
Thanking you in advance,
Friend is a model class, and you need an instance of such class in order to relate it to another model instance as a foreign relationship.
For example:
friend_instance = Friend.objects.get(name='Madeleaine')
...
friend_user = friend_instance.to_user.id

Insert or update on table violates foreign key constraint (error)

I am trying to save form data into a postgres table 'Student_Events' that is linked via foreign key to the 'Student_INFO table' but keep on receiving an integrity error:
'insert or update on table "entry_student_event" violates foreign key constraint "entry_student_event_nsn_id_2499e7fd_fk_entry_student_info_id"
DETAIL: Key (nsn_id)=(123556789) is not present in table "entry_student_info".'
There is one student in Student_INFO that has the nsn '123556789' so I am unsure to why it "is not present" in that table. Any help would be greatly appreciated as I am quite new to Django and PostgresSQL,
thanks
Views.py:
def Grouplist(request):
student_list = Student_INFO.objects.order_by('name')
for student in student_list:
if request.method == 'POST':
form = EntryForm(request.POST)
context = {
'student_list':student_list,
'form': form
}
if form.is_valid():
event_save = form.save(commit=False)
event_save.nsn_id = student.nsn
event_save.save()
return redirect('grouplist')
else:
form = EntryForm()
context = {
'student_list':student_list,
'form': form
}
return render(request, 'grouplist.html', context)
Models.py:
class Student_INFO(models.Model):
nsn = models.IntegerField(blank = False)
birthdate = models.DateField("BirthDate", blank = False)
name = models.CharField(max_length=30, blank = False)
age_catagory = models.CharField(max_length=8, blank = True, default = '')
grouproom = models.CharField(max_length=3, blank = False)
year_lvl = models.IntegerField(blank = False)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
def __str__(self):
return self.name
class Student_Event(models.Model):
nsn = models.ForeignKey(Student_INFO, on_delete=models.CASCADE)
event1 = models.CharField(max_length=15, blank=False, default = '')
event2 = models.CharField(max_length=15, blank=False, default = '')
event3 = models.CharField(max_length=15, blank=False, default = '')
event4 = models.CharField(max_length=15, blank=False, default = '')
event5 = models.CharField(max_length=15, blank=False, default = '')
event6 = models.CharField(max_length=15, blank=False, default = '')
def __str__(self):
return self.nsn
Forms.py
class EntryForm(forms.ModelForm):
class Meta:
model= Student_Event
fields= ["event1", "event2", "event3", "event4", "event5", "event6"]
widgets = {
'event1':forms.Select(choices=EVENT_CHOICES),
'event2':forms.Select(choices=EVENT_CHOICES),
'event3':forms.Select(choices=EVENT_CHOICES),
'event4':forms.Select(choices=EVENT_CHOICES),
'event5':forms.Select(choices=EVENT_CHOICES),
'event6':forms.Select(choices=OPEN_CHOICES),
}
Returns the error:
insert or update on table "entry_student_event" violates foreign key constraint "entry_student_event_nsn_id_2499e7fd_fk_entry_student_info_id"
DETAIL: Key (nsn_id)=(123556789) is not present in table "entry_student_info".
Thank you #BearBrown for solving it! student.nsn should be student.pk.

Save a foreign key in postgreSQL using python-django

I'm trying to a save a foreign key inside an object into my db using a form, but i get the error : 'Syntax isn't valid for integer', I discovered that postgreSQL save the foreign key as an id, how can i save it then?
Here it is my code.
Models.py :
class treballador(models.Model):
nom = models.CharField(max_length=150, null=False, unique=True)
cognom = models.CharField(max_length=150, null=False)
tipusDocID = models.CharField(max_length=3, choices=TIPUSDOC, null=False)
docId = models.CharField(max_length=9, null=False)
tlf_regex = RegexValidator(regex=r'^\d{9,9}$',message="Phone number must be entered in the format: '+999999999'. Up to 9 digits allowed.")
tlf = models.CharField(validators=[tlf_regex], blank=True, max_length=9) # validators should be a list
correu = models.EmailField(max_length=254)
ciutat = models.CharField(max_length=150)
dataDAlta = models.DateTimeField(auto_now_add=True)
def __unicode__(self):
return unicode(self.nom) or unicode(self.id)
class despesa(models.Model):
nomTreballador = models.ForeignKey(treballador, to_field='nom')
tipusDeGast = models.CharField(max_length=3, choices=GASTOS)
quantia = models.DecimalField(max_digits=5, decimal_places=2)
data = models.DateTimeField()
forms.py:
class desModelForm(forms.ModelForm):
data = forms.DateField(widget=DateInput(format='%d/%m/%Y'), label="Data de la despesa", input_formats=['%d/%m/%Y'])
class Meta:
model= despesa
fields= ["nomTreballador","tipusDeGast","quantia","data"]
def clean_despesa(self):
despeses = self.cleaned_data.get("tipusDeGast")
return despeses
def clean_date(self):
date = self.cleaned_data.get("data")
return date
def clean_quantia(self):
quantia = self.cleaned_data.get("quantia")
return quantia
def clean_nom(self):
nomTreballador = self.cleaned_data.get("nomTreballador")
return nomTreballador
def __init__(self, *args, **kwargs):
super(desModelForm, self).__init__(*args, **kwargs)
self.fields["nomTreballador"].queryset=treballador.objects.all().distinct()
views.py:
def home(request):
form = desModelForm(request.POST or None)
context = {
"gast_form": form
}
if form.is_valid():
desp = form.save(commit=False)
desp.save()
return render(request, "imputacioDespeses.html", context)
Your foreign key field is the problem.
Remove to_field and Django will automatically map it to ID
nomTreballador = models.ForeignKey(treballador)
Remove to_field='nom' from the nomTreballador ForeignKey field and it will insert the Treballador's primary key (an integer) instead of nom (which is a string).