Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AutoOneToOne doesn't work #51

Open
vorlif opened this issue Apr 7, 2016 · 14 comments
Open

AutoOneToOne doesn't work #51

vorlif opened this issue Apr 7, 2016 · 14 comments

Comments

@vorlif
Copy link

vorlif commented Apr 7, 2016

System

OS: Linux
Python: 3.5.1
Django: 1.9.5
Annoying: 0.9.0

Example

From README.md.

from annoying.fields import AutoOneToOneField

class MyProfile(models.Model):
    user = AutoOneToOneField(User, primary_key=True)
    home_page = models.URLField(max_length=255, blank=True)
    icq = models.IntegerField(blank=True, null=True)

Description

I have try your example but it doesnt't work. I have no error messages. The field works lika a regular OneToOne field.

@sparkplugmarcus
Copy link

sparkplugmarcus commented May 22, 2016

It is not working for me either.

class Alpha(models.Model):
  ...

class Beta(models.Model):
   alpha = AutoOneToOneField(Alpha, primary_key=True, on_delete=models.CASCADE)

I created an Alpha object and no corresponding Beta object was created.

@skorokithakis
Copy link
Owner

Hmm, can anyone see what the problem is, or submit a PR to fix it?

@arthurio
Copy link

arthurio commented Aug 4, 2016

@pymarco I'm just seing this but AutoOneToOneField won't create a related object unless you try to access it.

a = Alpha.objects.create()
Beta.objects.count()
# 0
a.beta
# <Beta: Beta object>
Beta.objects.count()  # 1

@usunyu
Copy link

usunyu commented Sep 1, 2016

@arthurio thanks, its worked for me on Django 1.10, python 2.7

@littlehome-eugene
Copy link

It is not working for me either.

django 1.9.7
annoying: 0.9.0

@arthurio
Copy link

@littlehome-eugene We are going to need more details than that to figure out what is not working for you...

@moorchegue
Copy link

Same here. Django 1.11.2, annoying 0.10.3.

Under some circumstances objects are being created, but what those are I haven't figured out yet.

@eduardocesar
Copy link

Not working for me either.
Django 1.11.6, python 2.7, annoying 0.10.3.

@arthurio
Copy link

@eduardocesar @moorchegue I don't see how we can possibly help you without more details... Did you read my previous comment ?

@eduardocesar
Copy link

@arthurio I've tried to access the related object after the creation of the primary object and did not appear. With some investigation, it seems that it was a boolean field in the related object that was preventing it's creation.

@sdawodu
Copy link

sdawodu commented Dec 20, 2017

Doesn't work for me either:

class PatientRecord(models.Model):

	checkup_interval = models.IntegerField(default=180)

class Patient(models.Model):
        name = models.CharField(max_length=64)
	# date_of_birth = models.DateField()
	record = AutoOneToOneField(PatientRecord, on_delete=models.CASCADE)

	registration_date = models.DateField()
	last_visit = models.DateField(blank=True, null=True)

	# Keep track of how many times we're sending messsages to people
	# to make sure we don't spam them
	appointment_reminder_attempt = models.IntegerField(default=0)

	email_address = models.EmailField(blank=True, null=True)

Creating a Patient object does not automatically create a PatientRecord. I've tried creating via the admin as well as via the shell. Doesn't seem to work either way.

Django==2.0
django-annoying==0.10.3

@hiamandeep
Copy link

hiamandeep commented Feb 22, 2018

@arthurio Thanks, accessing the related object creates it.

http://www.techinfected.net/2018/02/automatically-create-onetoonefield-in-django.html

@albrnick
Copy link

albrnick commented Feb 6, 2019

Is primary_key=True required for this to work?

My model is defined as such:

class CrmCategory(models.Model):
    stats_list_direct = AutoOneToOneField('CrmCategoryStatsList',
                                          null = True,
                                          related_name = 'direct_category',
                                          help_text = 'What stats to list, as defined by this category directly.')
    stats_list_union = AutoOneToOneField('CrmCategoryStatsList',
                                         null = True,
                                         related_name = 'union_category',
                                         help_text = 'An or''ing of the direct stats, and the children''s stats_list_union')


And yet when I access it, I get a None:

    cat = CrmCategory.objects.all().first()
    print('union',  cat.stats_list_union)

@arthurio
Copy link

arthurio commented Feb 8, 2019

@albrnick You want to put the AutoOneToOneField on the CrmCategoryStatsList model instead:

class CrmCategoryStatsList(models.Model):
    category = AutoOneToOneField(...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests