Home > Blockchain >  How do I get the id of a Foreign Key
How do I get the id of a Foreign Key

Time:03-28

I am trying to autofill these fields if the foreign key is selected

The Base Class:

class ScreeningCamp(models.Model):
    beneficiary=models.ForeignKey(Beneficiary,on_delete=models.CASCADE)
    name=models.CharField(max_length=200,default=Beneficiary.objects.get(id=beneficiary.id).name,blank=True)
    dob=models.DateField(default=Beneficiary.objects.get(id=beneficiary.id).dob,blank=True)
    gender=models.CharField(max_length=200,choices=GENDER_CHOICES,default=Beneficiary.objects.get(beneficiary.id).gender,blank=True)

The Parent Class:

class Beneficiary(models.Model):
    image=models.ImageField(upload_to='home/images/',blank=True,null=True)
    name = models.CharField(max_length=200)
    gender=models.CharField(max_length=6,choices=GENDER_CHOICES,default='male')
    dob=models.DateField()

I just want to autofill name, dob etc from the beneficiary class when the foreign key is selected

When it is not selected we have to manually do it

I think just getting the id of the selected object will help

Can anyone please tell me how to do that?

CodePudding user response:

So basically, You want to create a ScreeningCamp record with the data that is being used while creating the Beneficiary record.

example: let's only take name and dob fields.

beneficiary_1 = Beneficary(name='test1',dob=2006-10-02)

then a row on the ScreeningCamp Table will be created with the same name, dob, and a foreign key relationship to the beneficary_1.

You can achieve this by using Django signals.

models.py Let's only take name and dob for now.

Models on the models.py

class Beneficiary(models.Model):
    name = models.CharField(max_length=200)
    dob = models.DateField()


class ScreeningCamp(models.Model):
    beneficiary = models.ForeignKey(Beneficiary, on_delete=models.CASCADE)
    name = models.CharField(max_length=200, blank=True)
    dob = models.DateField(blank=True)

Assuming the models are declared on the same app's models.py. Create a signals.py file in the app that receives the post_save signal when a Beneficiary record is created. Let's call the app Post.

from django.db.models.signals import post_save
from django.dispatch import receiver
from . import models


@receiver(post_save, sender=models.Beneficiary)
def create_screening_camp(sender, instance, created, **kwargs):
    if not created:
        return
        # Create the ScreeningCamp object, only if it is newly created
    screening_camp = models.ScreeningCamp(name=instance.name, beneficiary=instance, dob=instance.dob)
    screening_camp.save()

on your apps.py


from django.apps import AppConfig


class PostConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'Post'

    def ready(self):
        from . import signals

in settings.py installed_apps, include

INSTALLED_APPS = [
    ...
    'Post.apps.PostConfig',
]

CodePudding user response:

If you're calling ScreeningCamp, Django can lookup the Foreignkey. Here is an example:

screening_camp = ScreeningCamp.objects.get(pk=1)
name = screening_camp.beneficiary.name
gender = screening_camp.beneficiary.gender
dob = screening_camp.beneficiary.dob
# populating form
form = ScreeningCampForm(initial={"name": name, "gender": gender, "dob":dob}, instance=screening_camp)
return render(request, "form.html", {"form": form})
  • Related