django models

35 views
Skip to first unread message

Soumen Khatua

unread,
May 6, 2019, 7:03:45 AM5/6/19
to django...@googlegroups.com
Hi Folks,

How this underline code works?


models.py

class Address(models.Model):
    first_name = models.CharField(max_length=256, blank=True)
    last_name = models.CharField(max_length=256, blank=True)
    company_name = models.CharField(max_length=256, blank=True)
    street_address_1 = models.CharField(max_length=256, blank=True)
    street_address_2 = models.CharField(max_length=256, blank=True)
    city = models.CharField(max_length=256, blank=True)
    city_area = models.CharField(max_length=128, blank=True)
    postal_code = models.CharField(max_length=20, blank=True)
    country = CountryField()
    country_area = models.CharField(max_length=128, blank=True)
    phone = PossiblePhoneNumberField(blank=True, default='')

     def __eq__(self, other):
               return self.as_data() == other.as_data()
     __hash__ = models.Model.__hash__


Thank you.



        


   
  




Skyisblue

unread,
May 6, 2019, 4:50:26 PM5/6/19
to django...@googlegroups.com
Soumen,

The function looks to compare two Address objects. The as_data() call is custom made. I saw it documented in your other post. It removes the Primary Key and User from the Address object. I presume this is so they can compare the hash of one address to the hash of another.  

Regards,
Joe

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAPUw6WY-pNGnPBHzDA%3DiRZUHY7NrmK30_dMbKiQpe%2B1n%2ByW%2BQQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Soumen Khatua

unread,
May 7, 2019, 2:22:43 AM5/7/19
to django...@googlegroups.com
Yes,You are right as_data is custom made function inside the same class,here is the code:



def as_data(self):
        """Return the address as a dict suitable for passing as kwargs.Result does not contain the primary key or an associated user."""
        data = model_to_dict(self,exclude=['id','user'])
        if isinstance(data['country'], Country):
            data['country'] = data['country'].code
            if isinstance(data['phone'], PhoneNumber):
                #as_e164 is a phonenumber format information
                data['phone'] = data['phone'].as_e164
        return data


But here other is an argument so we need to pass it then without passing how we can compare with self.as_data varibale?

Thank you for your response.

Soumen Khatua

unread,
May 7, 2019, 3:05:33 AM5/7/19
to django...@googlegroups.com
Hi Folks,

I didn't  understand this code please help me to understand this code.

class AddressQueryset(models.QuerySet):
    def annotate_default(self, user):
        # Set default shipping/billing address pk to None
        # if default shipping/billing address doesn't exist
        default_shipping_address_pk, default_billing_address_pk = None, None
        if user.default_shipping_address:
            default_shipping_address_pk = user.default_shipping_address.pk
        if user.default_billing_address:
            default_billing_address_pk = user.default_billing_address.pk
        return user.addresses.annotate(
        user_default_shipping_address_pk=Value(
        default_shipping_address_pk, models.IntegerField()),
        user_default_billing_address_pk=Value(
                default_billing_address_pk, models.IntegerField()))


default_shipping_address & default_billing_address_pk is available in my User table it is a foreignkey of Address table  and  addresses is also avialable in my User table as ManytoManytoField refering to same Address Table and the last i'm using this inside Address table just like this :
objects = AddressQueryset.as_manager()



Thank You

sachinbg sachin

unread,
May 7, 2019, 3:59:04 AM5/7/19
to django...@googlegroups.com
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages