# Changing a foreign key on an object inside admin interface

**URL:** <https://forum.djangoproject.com/t/changing-a-foreign-key-on-an-object-inside-admin-interface/24256>\
**Category:** The Admin\
**Created:** [October 2, 2023, 9:01am UTC](https://forum.djangoproject.com/t/changing-a-foreign-key-on-an-object-inside-admin-interface/24256 "2023-10-02T09:01:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![danjirovec](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/danjirovec/32/16011_2.png) [@danjirovec](https://forum.djangoproject.com/u/danjirovec)\
**Post date:** [October 2, 2023, 9:01am UTC](https://forum.djangoproject.com/t/changing-a-foreign-key-on-an-object-inside-admin-interface/24256/1 "2023-10-02T09:01:48Z")

</div>

Hi,

in my admin interface, I want to change a foreign key of a model from a dropdown. This foreign key is a part of composite primary key. However when I try to change the foreing key, django creates a new object, it doesn’t modify the existing one. Why is that happening and how can I change the foreign key of the current model. I’m using PostgreSQL on the backend.

Here is my model:

```auto
class CropStageHasCondition(models.Model, GenericCheckForDelete):
    id = CompositeKey(columns=['crop_stage_id', 'condition_id'])
    crop_stage = models.ForeignKey(CropStage, models.DO_NOTHING)
    condition = models.ForeignKey(Condition, models.DO_NOTHING)
    threshold_low = models.FloatField(blank=True, null=True)
    threshold_high = models.FloatField(blank=True, null=True)
    threshold_mismatch_impact = models.ForeignKey('ThresholdMismatchImpact', models.DO_NOTHING, blank=True, null=True)
    health_threshold_low = models.FloatField(blank=True, null=True)
    health_threshold_high = models.FloatField(blank=True, null=True)
    health_threshold_mismatch_impact = models.ForeignKey('ThresholdMismatchImpact', models.DO_NOTHING, related_name='cropstagehascondition_health_threshold_mismatch_impact_set', blank=True, null=True)
    relevant_for_calculation = models.BooleanField()

    class Meta:
        managed = False
        db_table = 'planner"."crop_stage_has_condition'
        unique_together = (('crop_stage', 'condition'),)

    def __str__ (self) -> str:
        return f"{self.crop_stage} - {self.condition}"

    def clean(self):
        existing_objects = CropStageHasCondition.objects.filter(
            crop_stage=self.crop_stage,
            condition=self.condition
        )
        if existing_objects.exists():
            raise ValidationError({'crop_stage': "CropStageHasCondition with this crop_stage and condition already exists.",
                                   'condition': "CropStageHasCondition with this condition and crop_stage already exists."})

```

For composite key I’m using **viewflow** library.

Thanks for your help.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [October 2, 2023, 11:45am UTC](https://forum.djangoproject.com/t/changing-a-foreign-key-on-an-object-inside-admin-interface/24256/2 "2023-10-02T11:45:53Z")

</div>

> [@danjirovec](#):
>
> Why is that happening and how can I change the foreign key of the current model.

It’s happening because that’s how the ORM is designed and that’s how it’s documented. (See [primary\_key](https://docs.djangoproject.com/en/4.2/ref/models/fields/#primary-key))

Also, I suggest you don’t try to use a composite primary key. There are reasons why Django doesn’t currently support this. It’s a very complex issue that has been hanging around for a _long_ time.  
See:

- [https://code.djangoproject.com/ticket/373](https://code.djangoproject.com/ticket/373)
- [MultipleColumnPrimaryKeys – Django](https://code.djangoproject.com/wiki/MultipleColumnPrimaryKeys)
