How to input data from one Django admin to another model that is not related by a model relationship?

I have 3 models. First model is RAP, which act as a Master data. the code model details below:

    class RAP(models.Model):
        nama_proyek = models.CharField(max_length=255,unique=True)
        jenis_proyek = models.CharField(max_length=255,null=True,blank=True)
        pekerjaan_persiapan = models.BigIntegerField(blank=True, null=True, verbose_name="Pekerjaan Persiapan")
        pekerjaan_galian_tanah = models.BigIntegerField(blank=True, null=True, verbose_name='Pekerjaan Galian Tanah, Pasir Urug & Lantai Kerja')
        pekerjaan_pondasi = models.BigIntegerField(blank=True, null=True, verbose_name='Pekerjaan Pondasi Tiang Pancang & Beton Tapak')
        pekerjaan_cor_beton_pile_cap = models.BigIntegerField(blank=True, null=True, verbose_name='Pekerjaan Cor Beton Pile Cap')
        pekerjaan_cor_beton_slug = models.BigIntegerField(blank=True, null=True, verbose_name='Pekerjaan Cor Beton Slug')
        pekerjaan_pelat_beton_lantai = models.BigIntegerField(blank=True, null=True, verbose_name='Pekerjaan Pelat Beton Lantai')
        pekerjaan_pelat_bawah = models.BigIntegerField(blank=True, null=True,verbose_name='Pekerjaan Pelat Bawah Retaining Wall')
        pekerjaan_dinding = models.BigIntegerField(blank=True, null=True,verbose_name='Pekerjaan Dinding')
        pekerjaan_konstruksi_baja = models.BigIntegerField(blank=True, null=True,verbose_name='Pekerjaan Konstruksi Baja')
        pekerjaan_penutup = models.BigIntegerField(blank=True, null=True,verbose_name='Pekerjaan Penutup Atap dan Lain-lain')
        
        class Meta:
            verbose_name_plural = "I. Master RAP" 
            
        def __str__(self):
            return str(self.nama_proyek)

and then i have a ForeignKey model from RAP called PekerjaanPersiapan, the details below:

class PekerjaanPersiapan(models.Model):
    nama_proyek = models.ForeignKey(RAP, on_delete=models.CASCADE)
    pekerjaan = models.CharField(max_length=255,blank=True, null=True)
    satuan = models.CharField(max_length=255,blank=True, null=True)
    volume = models.FloatField(blank=True,null=True)
    harga_satuan = models.FloatField(blank=True,null=True)
    sub_total = models.BigIntegerField(blank=True,null=True)
    catatan = models.CharField(max_length=255,blank=True,null=True)
    sub_total_harga = models.BigIntegerField(blank=True,null=True)
    
    class Meta:
        verbose_name_plural = "Pekerjaan Persiapan"
        
    def __str__(self):
        return self.nama_proyek

and now i have the third model called DataMasterPekerjaanPersiapan, which later i want to use the data in this model to be inputted to the PekerjaanPersiapan model. The details below:

class DataMasterPekerjaanPersiapan(models.Model):
    kode = models.CharField(max_length=20)
    deskripsi = models.CharField(max_length=200)
    jumlah_bahan = models.IntegerField(null=True,blank=True)
    jumlah_tenaga = models.IntegerField(null=True,blank=True)
    jumlah_total = models.IntegerField(null=True,blank=True)
    keuntungan = models.FloatField(null=True,blank=True,default=10)
    jumlah_setelah_keuntungan = models.IntegerField(null=True,blank=True)
    dibulatkan = models.IntegerField(null=True,blank=True)
    
    class Meta:
        verbose_name_plural = "III. Data  Master Pekerjaan Persiapan"
        
    def __str__(self):
        return self.deskripsi

i use django admin to do all these task. model PekerjaanPersiapan is an inline admin for RAP.

the admin code is below.


class DataMasterPekerjaanPersiapanAdmin(admin.ModelAdmin):
    list_display = ('kode','deskripsi','jumlah_bahan','jumlah_tenaga','jumlah_total','keuntungan','jumlah_setelah_keuntungan','dibulatkan')
    list_display_links = ('kode','deskripsi')
    readonly_fields = ('jumlah_bahan','jumlah_tenaga','jumlah_total','keuntungan','dibulatkan','jumlah_setelah_keuntungan')
    
    
    inlines = [DataPekerjaanPersiapanSegmenBahanAdmin,DataPekerjaanPersiapanSegmenPekerjaAdmin]
    
    def hitung_jumlah_bahan_dan_tenaga(self, instance):
        instance.jumlah_bahan = DataPekerjaanPersiapanSegmenBahan.objects.filter(kode__kode=instance.kode).aggregate(total=Sum('jumlah_harga'))['total']
        instance.jumlah_tenaga = DataPekerjaanPersiapanSegmenPekerja.objects.filter(kode__kode=instance.kode).aggregate(total=Sum('jumlah_harga'))['total']

    def hitung_jumlah_total(self, instance):
        instance.jumlah_total = (instance.jumlah_bahan or 0) + (instance.jumlah_tenaga or 0)
        instance.jumlah_setelah_keuntungan = instance.jumlah_total +  (instance.jumlah_total * (instance.keuntungan/100))
        instance.dibulatkan = ((instance.jumlah_setelah_keuntungan)//100)*100

    def save_related(self, request, form, formsets, change):
        super().save_related(request, form, formsets, change)
        self.hitung_jumlah_bahan_dan_tenaga(form.instance)
        self.hitung_jumlah_total(form.instance)
        form.instance.save()

admin.site.register(DataMasterPekerjaanPersiapan, DataMasterPekerjaanPersiapanAdmin)

now for the problem part is, i dont have any field relation between DataMasterPekerjaanPersiapan with either PekerjaanPersiapan or RAP, but i need to input the “proyek_name” field in DataMasterPekerjaanPersiapan, to know which data i want to input (or we can say to identify the data from DataMasterPekerjaanPersiapan is belong to which RAP).

btw, how i input data from DataMasterPekerjaanPersiapan to PekerjaanPersiapan is by actions item in django admin. so when i pick some data from the list, i can do an “input data to PekerjaanPersiapan” action.

I tried to input the field proyek_name by using

rap_obj = RAP.objects.first()
            
            PekerjaanPersiapan.objects.create(
                nama_proyek_id=rap_obj.nama_proyek,
            )

but it gives me error:
“Field ‘id’ expected a number but got ‘PIK’.”

but if i input the id of the RAP:

rap_obj = RAP.objects.first()
            
            PekerjaanPersiapan.objects.create(
                nama_proyek_id=rap_obj.id,
            )

when i open the form data of RAP, it gives me this error:
str returned non-string (type RAP)”

actually i dont understand why it gives this error. can anyone explain?
and for the main question is so how to input the name of object that dont have a relation in this case? the PekerjaanPersiapan Object hasn’t even created yet.

lol im sorry but i dont know why my code section that using ‘’’ ‘’’ is not working properly.

Hey there!
It’s because you’re using the wrong separator. The correct one is `.
You make it easier by clicking on the button circled on the image bellow.
You can either click it first and then past the code in, or paste, select the code and click on the button.
image

Thankyou for the information bro! appreciate it! ah btw can u please check on my question too?

When you’re referencing a ForeignKey, you have two effective options - either the name of the ForeignKey, or the name of the ForeignKey with using the _id suffix.
When you’re using just the name of the ForeignKey, you need to assign an instance of the object to that field.
When you’re using the name of the field with the _id suffix, you need to assign the id value.

In other words, if you have a field defined as:
a_field = ForeignKey(SomeModel, ...)
and in some code somewhere else you have:
some_model = SomeModel.objects.get(id=1)

Then the following two assignments are valid:

a_field = some_model
a_field_id = some_model.id

but the following two statements are not valid:

a_field = some_model.id
a_field_id = some_model

thankyou, i’ve solved my problem, i use a query like this

rap_obj = RAP.objects.first()
            PekerjaanPersiapan.objects.create(
                nama_proyek_id=rap_obj.id,
            )

thanks for the help bro!