# Prevent duplicate entries on user level

**URL:** <https://forum.djangoproject.com/t/prevent-duplicate-entries-on-user-level/29444>\
**Category:** Forms & APIs\
**Created:** [March 24, 2024, 5:41pm UTC](https://forum.djangoproject.com/t/prevent-duplicate-entries-on-user-level/29444 "2024-03-24T17:41:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![captlemo](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/captlemo/32/20118_2.png) [@captlemo](https://forum.djangoproject.com/u/captlemo)\
**Post date:** [March 24, 2024, 5:41pm UTC](https://forum.djangoproject.com/t/prevent-duplicate-entries-on-user-level/29444/1 "2024-03-24T17:41:05Z")

</div>

Hello!  
I am looking to write a validator that will prevent a user from inputting duplicate entries, but would allow duplicate entries in the overall table. For example, let’s say each of my users is a company that needs to input customer info. If two of these companies have the same customer, they should be able to have the same data in their own user-owned rows without throwing an error. On the other hand, I’d like to prevent an individual user from creating duplicates. I had tried the unique\_together option on the model only to have it fail because of the owner field being a foreign key. Additionally, the owner field in the form is excluded. I have read through a bunch of the documentation and am still a bit lost. I am new to coding and Django so if anyone could offer some insights, it would be greatly appreciated. TIA

```auto
views.py
class CustomerCreate(OwnerCreateView):
    model = Customer
    form_class = CustomerCreateForm
    template_name = ...
    success_url = ...
    
class CustomerUpdate(OwnerUpdateView):
    model = Customer
    form_class = CustomerUpdateForm
    template_name = ...
    success_url = ...
	
owner.py
class OwnerCreateView(LoginRequiredMixin, CreateView):
    def form_valid(self, form):
        form.instance.owner = self.request.user
        return super().form_valid(form)

class OwnerUpdateView(LoginRequiredMixin, UpdateView):
    def get_queryset(self):
        qs = super(OwnerUpdateView, self).get_queryset()
        return qs.filter(owner=self.request.user)
		
models.py
class Customer(models.Model:
	first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    company = models.CharField(max_length=128, blank=True)
	owner = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
		
forms.py
class CustomerCreateForm(ModelForm):
	class Meta:
		model = Customer
		exclude = ('owner',)
		
class CustomerUpdateForm(ModelForm):
	class Meta:
		model = Customer
		exclude = ('owner',)

```

---

<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:** [March 24, 2024, 5:46pm UTC](https://forum.djangoproject.com/t/prevent-duplicate-entries-on-user-level/29444/2 "2024-03-24T17:46:06Z")

</div>

> [@captlemo](#):
>
> I had tried the unique\_together option on the model only to have it fail because of the owner field being a foreign key.

That should work. The owner field being a foreign key should not prevent its usage in a `unique_together` clause. Please post the code you tried along with the complete error (and traceback) you received while trying it.  
(Or, more appropriately, you should probably be using a [`UniqueConstraint`](https://docs.djangoproject.com/en/5.0/ref/models/constraints/#uniqueconstraint). See [Model Meta options | Django documentation | Django](https://docs.djangoproject.com/en/5.0/ref/models/options/#unique-together))

---

<div class="post-metadata">

**Author:** ![captlemo](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/captlemo/32/20118_2.png) [@captlemo](https://forum.djangoproject.com/u/captlemo)\
**Post date:** [March 24, 2024, 6:13pm UTC](https://forum.djangoproject.com/t/prevent-duplicate-entries-on-user-level/29444/3 "2024-03-24T18:13:54Z")

</div>

When I first tried the code below in models.py, it would blow up. I then simplified my form to the code mentioned above to avoid confusing myself with crispy-form stuff. I can no longer duplicate that condition. Currently, the unique\_together allows a duplicate post when the “owner” field is listed but If I take “owner” out, it will produce and error, but for all users. I did not know about UniqueConstraint and will read up on it. Thank you for your help!

```auto
class Customer(models.Model:
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    company = models.CharField(max_length=128, blank=True)
    owner = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)

    class Meta:
        unique_together = ["first_name", "last_name", "owner"]

```

```auto

```

---

<div class="post-metadata">

**Author:** ![captlemo](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/captlemo/32/20118_2.png) [@captlemo](https://forum.djangoproject.com/u/captlemo)\
**Post date:** [March 25, 2024, 3:43am UTC](https://forum.djangoproject.com/t/prevent-duplicate-entries-on-user-level/29444/4 "2024-03-25T03:43:02Z")

</div>

After a bit more digging, I think the issue is with validation not running on excluded fields. I’ll have to figure out a work around tomorrow.
