ModelForm with OneToOneField in Django

You have to create second form for PrinterAddress and handle both forms in you view: if all((profile_form.is_valid(), address_form.is_valid())): profile = profile_form.save() address = address_form.save(commit=False) address.printer_profile = profile address.save() Of course in the template you need to show both forms under one <form> tag 🙂 <form action=”” method=”post”> {% csrf_token %} {{ profile_form }} {{ address_form … Read more

Why use a 1-to-1 relationship in database design?

From the logical standpoint, a 1:1 relationship should always be merged into a single table. On the other hand, there may be physical considerations for such “vertical partitioning” or “row splitting”, especially if you know you’ll access some columns more frequently or in different pattern than the others, for example: You might want to cluster … Read more

Check if a OneToOne relation exists in Django

So you have a least two ways of checking that. First is to create try/catch block to get attribute, second is to use hasattr. class A(models.Model): def get_B(self): try: return self.b except: return None class B(models.Model): ref_a = models.OneToOneField(related_name=”ref_b”, null=True) Please try to avoid bare except: clauses. It can hide some problems. The second way … Read more

Hibernate ManyToOne vs OneToOne

They look exactly the same on schema but there is difference on Hibernate Layer. If you try something like that: Address address = new Address(); Order order1 = new Order(); order1.setAddress(address); Order order2 = new Order(); order2.setAddress(address); save(); Everything will be OK. But, after save if you try get Order: @OneToOne case: org.hibernate.HibernateException: More than … Read more

JPA @OneToOne with Shared ID — Can I do this Better?

To map one-to-one association using shared primary keys use @PrimaryKeyJoinColumn and @MapsId annotation. Relevant sections of the Hibernate Reference Documentation: PrimaryKeyJoinColumn The PrimaryKeyJoinColumn annotation does say that the primary key of the entity is used as the foreign key value to the associated entity. MapsId The MapsId annotation ask Hibernate to copy the identifier from … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)