Hi Folks,
I'm getting one error from models.py,here is my models.py.

Error:
    from django.db.models import SubfieldBase
   ImportError: cannot import name 'SubfieldBase'

models.py
import uuid
from django.conf import settings
from django.db import models
from django.db.models import Q, Value
from django.forms.models import model_to_dict
from django.utils import timezone
from django.utils.translation import pgettext_lazy
from django_countries.fields import Country, CountryField

from django.contrib.auth.models import (
    AbstractBaseUser, BaseUserManager, PermissionsMixin)

from phonenumber_field.modelfields import PhoneNumber, PhoneNumberField
from versatileimagefield.fields import VersatileImageField
from .validators import validate_possible_number

# Create your models here.
class PossiblePhoneNumberField(PhoneNumberField):
    """Less strict field for phone numbers written to database."""
    default_validators = [validate_possible_number]

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()))

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='')
    objects = AddressQueryset.as_manager()

    class Meta:
        ordering = ('pk', )

    @property
    def full_name(self):
        return '%s %s' % (self.first_name, self.last_name)

    def __str__(self):
        if self.company_name:
            return '%s - %s' % (self.company_name, self.full_name)
        return self.full_name


    def __eq__(self, other):
        '''The equality method is defined such that instances with the same
primary key value and the same concrete class are considered equal, except
that instances with a primary key value of None aren’t equal to anything
except themselves.'''
        return self.as_data() == other.as_data()

    # The __hash__() method is based on the instance’s primary key value.
It is effectively hash(obj.pk). If the instance doesn’t have a primary key
value then a TypeError will be raised (otherwise the __hash__() method
would return different values before and after the instance is saved, but
changing the __hash__() value of an instance is forbidden in Python.
    __hash__ = models.Model.__hash__

    # model_to_dict will convert all the data into dict except id and user
in this model
    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


    def get_copy(self):
        """Return a new instance of the same address."""
        return Address.objects.create(**self.as_data())

class UserManager(BaseUserManager):
    def create_user(self, email, password=None, is_staff=False,
is_active=True,
            **extra_fields):
            """Create a user instance with the given email and password."""
            email = UserManager.normalize_email(email)
            # Google OAuth2 backend send unnecessary username field
            extra_fields.pop('username', None)

            user = self.model(email=email, is_active=is_active,
is_staff=is_staff,
            **extra_fields)

            if password:
                user.set_password(password)
            user.save()
            return user

    def create_superuser(self, email, password=None, **extra_fields):
        return self.create_user(
            email, password, is_staff=True, is_superuser=True,
**extra_fields)


    def customers(self):
        return self.get_queryset().filter(
            Q(is_staff=False) | (Q(is_staff=True) &
Q(orders__isnull=False)))

    def staff(self):
        return self.get_queryset().filter(is_staff=True)

def get_token():
    return str(uuid.uuid4())


class User(PermissionsMixin, AbstractBaseUser):
    email = models.EmailField(unique = True)
    first_name = models.CharField(max_length=256, blank=True)
    last_name = models.CharField(max_length=256, blank=True)
    addresses = models.ManyToManyField(
        Address, blank=True, related_name = 'user_addresses')
    is_staff = models.BooleanField(default = False)
    token = models.UUIDField(default=get_token, editable=False, unique=True)
    is_active = models.BooleanField(default=True)
    note = models.TextField(null=True, blank=True)
    date_joined = models.DateTimeField(default=timezone.now, editable=False)
    default_shipping_address = models.ForeignKey(
        Address, related_name='+', null=True, blank=True,
        on_delete=models.SET_NULL)
    default_billing_address = models.ForeignKey(
        Address, related_name='+', null=True, blank=True,
        on_delete=models.SET_NULL)
    avatar = VersatileImageField(
    upload_to='images', blank=True, null=True)
    USERNAME_FIELD = 'email'
    objects = UserManager()

    class Meta:
        permissions = (
            (
                'manage_users', pgettext_lazy(
                    'Permission description', 'Manage customers.')),
            (
                'manage_staff', pgettext_lazy(
                    'Permission description', 'Manage staff.')),
            (
                'impersonate_users', pgettext_lazy(
                    'Permission description', 'Impersonate customers.')))

    def get_full_name(self):
        if self.first_name or self.last_name:
            return ('%s %s' % (self.first_name, self.last_name)).strip()

        if self.default_billing_address:
            first_name = self.default_billing_address.first_name
            last_name = self.default_billing_address.last_name
            if first_name or last_name:
                return ('%s %s' % (first_name, last_name)).strip()
        return self.email

    def get_short_name(self):
        return self.email

    def get_ajax_label(self):
        address = self.default_billing_address
        if address:
            return '%s %s (%s)' % (
                address.first_name, address.last_name, self.email)
        return self.email
class CustomerNote(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL,blank=True, null=True,
        on_delete=models.SET_NULL)
    date = models.DateTimeField(db_index=True, auto_now_add=True)
    content = models.TextField()
    is_public = models.BooleanField(default=True)
    customer = models.ForeignKey(
        settings.AUTH_USER_MODEL, related_name='notes',
        on_delete=models.CASCADE)

    class Meta:
        ordering = ('date', )

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@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/CAPUw6WZOc2pWVSES6Vwn1%2BJeYAyhQvKfPrisw1Ggg9V_4480Pg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
  • Error Soumen Khatua

Reply via email to