Django models connections

i having issue on the django models.
Screenshot from 2024-01-26 11-57-31

Like i have 2 models of django in a one app names are detail and task so in the task app i had taken the detail model as a foreign key so whenever i select any saved attribute of detail field in task model then the same column like of detail field should be fill like name and email both are same so they both are filled same as of detail model attribute i selected
i have attached the screenshot of this

Please post the actual models involved here - it’s easier to talk about this in terms of the real code being used.

Also clarify if what you’re asking about here is something you want to do in the admin or in one of your views.

Side note: Do not post images of code here. Copy/paste the code into the body of your post, surrounded between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```.

class Detail(models.Model):
    name = models.CharField(max_length=30, unique=True)
    email = models.EmailField(max_length=50)
def __str__(self):
        return self.name
class Task(models.Model):
title = models.CharField(_("title"), max_length=200)
detail = models.ForeignKey(Detail, verbose_name=_('Detail'), on_delete=models.SET_NULL, null=True, blank=True)
    name = models.CharField(max_length=30, unique=True)
    email = models.EmailField(max_length=50)
def __str__(self):
        return self.title

in this in the task model when i select any id from detail model as it seen by you that its taken as a foreign key then the name and email attribute should be filled automatically in the task model

this thing should be done in my admin page of django.

Do a task’s name and email always have the same value as the related detail’s ones ?

If so, then do not define name and email field on Task model, they are an unnecessary redundancy of the related detail’s fields : if you need a task name or email, you could still use task.detail.name / task.detail.email.

If not, can you explain in more details what is the behaviour you expect in the admin: do you want the name and email field the be pre-filled with the detail’s ones upon detail selection, but you can still change their value with something else ?

Need to show on task because of the requirement

And yesi want that prefilled with detail upon detail selection but user cant change the name and email in task.

you can still change their value with something else ? no i dont want to change it then

Do a task’s name and email always have the same value as the related detail’s ones ?yes they have the same value as per the selected id of detail model in the task model

So, remove those fields from the Task model.

When you need to display the email/name for a task, use task.detail.name or task.detail.email. You can also define properties on Task model shadowing the access to underlying detail relation.

class Task(models.Model):
    title = models.CharField(_("title"), max_length=200)
   
    detail = models.ForeignKey(Detail, verbose_name=_('Detail'), on_delete=models.SET_NULL, null=True, blank=True)
    name = models.CharField(max_length=30, unique=True)
    email = models.EmailField(max_length=50)

class Detail(models.Model):
    name = models.CharField(max_length=30, unique=True)
    email = models.EmailField(max_length=50)

tell me where should i applied that in code and do i need to apply changes in admin.py file?

Remove the two following lines in Task model

Whether you have to change things or not in the admin depends on how you defined it. If things don’t work after cleaning the model, share your admin definition code so that we can see how to update

if i remove then how can i see whether name and email are added or not, i want to see that in my django admin like the attach pic
Screenshot from 2024-01-27 15-28-46

You don’t add name or email to a Task. You add a Detail, which holds the name and email.

In the details select widget, you already see the name. If you want to see the detail name AND email when selecting it in the list, you can change the __str__ definition of Detail model. E.g.

def __str__(self):
    return f"{self.name} ({self.email})"

so i put these in detail model or task model?

Let’s share the state of your models according to above suggestions.

After removing name and email fields from Task model, how does your admin looks like ? Is there anything missing in the admin ?

that both column is removed from admin panel of task

i want that to verify whether the same has been putted here or not

What do you mean by “the same has been putted here” ?

What do you want to verify ?

Let me tell you with an example

suppose in detail model i have attribute like name email and address, and in task model i have attribute like name, email, phone and detail( whole model as a foreign key) so when in a django admin panel i field detail model’s name email and address then i go to task model to fill things so when i go to there and the first attribute is a detail model(foreignkey) is like a drop down so when i selected the value that i filled on it so in that there is name and email field too, so when i select that field of detail model in task model then in the task model there is name and email field too so it should be filled same as the selected detail field and that should be seen by admin(me) in task model.

Screenshot from 2024-01-29 10-34-42

Once again, if the name/email fields of a Task instance always have the same values as the name/email fields of the related Detail instance, then you are creating an unnecessary redundancy of information in your database. Doing this, you even can’t ensure that nobody will change the name/email of the Detail instance, that will lead to inconsistency in your database because Task instance won’t update in the same time.

Focusing on the creation of Task in the admin, what you want cannot be achieved natively in Django admin as it would require some javascript to automatically update the name/email field upon Detail selection. Moreover, if the email/name fields of the Task should not have a different value than the selected Detail, those fields should be readonly, that they are not.

its ok, if user can’t change it because this task and detail field are going to be filled by the admin only(me) but i want to see that iname and email as same as in detail in task field too.(mandatorily)

There are a couple thoughts that come to my mind:

First, you really should re-evaluate your model design. antoinehumbert is correct in that what you’ve created and are intending to do is unnecessary. You should take a step back and look at the situation from a broader perspective.

Second, this can be done in the admin using a custom media component.